Problem with basic usage of std::enable_if

▼魔方 西西 提交于 2020-05-09 06:06:26

问题


I'm trying to make a simple template function that given some parameters, it outputs them with a space in between. Some of those can be elements of an enum, and in that case I want to output its integer value.

This is the code I have:

#include <iostream>
#include <type_traits>
using std::cerr;
using std::endl;

// Output integer value if parameter is an element of an enum
template<typename T, typename = typename std::enable_if_t<std::is_enum<T>::value>>
constexpr std::ostream& debug(const T& a) {
  cerr << (int)(a);
  return cerr;
}

// Output parameter otherwise
template<typename T, typename = typename std::enable_if_t<!std::is_enum<T>::value>>
constexpr std::ostream& debug(const T& a) {
  cerr << a;
  return cerr;
}

// Print parameters separated by a space
template<typename T, typename ...U>
constexpr std::ostream& debug(const T& a, const U&... b) {
  debug(a) << ' ';
  debug(b...);
  return cerr;
}

template<typename ...U>
constexpr void debug(const U&... a) {
  debug(a...) << std::endl;
}

enum class Animal{Dog, Cat};

int main() {
  debug(Animal::Dog);
  debug(Animal::Dog, Animal::Cat);
  debug("Hello");
  debug(100, 'A');
  debug(Animal::Dog, "Hello", "Bye");
}

Commenting the last three lines it says that the second function is a redeclaration of the first. Any ideas on how to solve this or why it does not work?


回答1:


Default template arguments are not part of the function definition. Use a dummy parameter instead, so that the second argument has a different type:

template<typename T, typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
constexpr std::ostream& debug(const T& a) {
  std::cerr << (int)(a);
  return std::cerr;
}

template<typename T, typename std::enable_if_t<!std::is_enum<T>::value, int> = 0>
constexpr std::ostream& debug(const T& a) {
  std::cerr << a;
  return std::cerr;
}


来源:https://stackoverflow.com/questions/57523977/problem-with-basic-usage-of-stdenable-if

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!