目标是函数func仅对枚举类型有声明,其他类型直接编译出错
#include <type_traits>
template<class T>
void func(typename std::enable_if<std::is_enum<T>::value, T>::type* objs);
enum Color { Red, Green, Blue };
int main()
{
Color c;
func(&c);
}
x.cc: In function ‘int main()’:
x.cc:11:16: error: no matching function for call to ‘func(Color*)’
func(&c);
^
x.cc:4:6: note: candidate: ‘template<class T> void func(typename std::enable_if<std::is_enum<_Tp>::value, T>::type*)’
void func(typename std::enable_if<std::is_enum<T>::value, T>::type* objs);
^~~~
x.cc:4:6: note: template argument deduction/substitution failed:
x.cc:11:16: note: couldn't deduce template parameter ‘T’
func(&c);
func<Color>(&c) 是能用的,但不是我想要的
--
修改:laputa2013 FROM 49.7.21.*
FROM 49.7.21.*