std::enable_if 的基本用法有问题

Problem with basic usage of std::enable_if

我正在尝试制作一个简单的模板函数,给定一些参数,它在它们之间输出 space。其中一些可以是枚举的元素,在这种情况下我想输出它的整数值。

这是我的代码:

#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");
}

最后三行的注释说第二个函数是第一个函数的重新声明。关于如何解决这个问题或为什么它不起作用的任何想法?

默认模板参数不是函数定义的一部分。请改用虚拟参数,以便第二个参数具有不同的类型:

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;
}