是否有可能专门为枚举的模板化方法?
像(下面的无效代码):
template <typename T>
void f(T value);
template <>
void f<enum T>(T value);
Run Code Online (Sandbox Code Playgroud)
在这种情况下这是不可能的,那么假如我有专业化的多种类型,如int,unsigned int,long long,unsigned long long,等等,那么其专业化的枚举值将使用?
MCVE:
#include <type_traits>
template<typename T>
bool func( typename std::enable_if< std::is_enum<T>::value, T >::type &t, int x )
{
}
enum class Bar { a,b,c };
int main()
{
Bar bar{Bar::a};
func(bar, 1);
}
Run Code Online (Sandbox Code Playgroud)
我希望func(bar, 1);能够匹配我对funcg ++报告的定义:
sfi.cc: In function 'int main()':
sfi.cc:13:17: error: no matching function for call to 'func(Bar&, int)'
func(bar, 1);
^
sfi.cc:13:17: note: candidate is:
sfi.cc:4:10: note: template<class T> bool func(typename std::enable_if<std::is_e
num<_Tp>::value, T>::type&, int)
bool func( typename std::enable_if< std::is_enum<T>::value, T >::type &t, int …Run Code Online (Sandbox Code Playgroud)