标准::enable_if的基本用法问题



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

这是我的代码:

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

最新更新