相关疑难解决方法(0)

枚举的模板专业化

是否有可能专门为枚举的模板化方法?

像(下面的无效代码):

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,等等,那么其专业化的枚举值将使用?

c++ enums templates template-specialization

8
推荐指数
2
解决办法
2万
查看次数

带有is_enum的enable_if不起作用

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)

c++ sfinae c++11

4
推荐指数
3
解决办法
3461
查看次数

标签 统计

c++ ×2

c++11 ×1

enums ×1

sfinae ×1

template-specialization ×1

templates ×1