M.M*_*M.M 3 c++ templates sfinae c++11
我目前有一个功能:
template<typename T>
bool func(T &t, int x)
{
// do stuff...
}
Run Code Online (Sandbox Code Playgroud)
但是我希望有三个不同的功能体:
T 是一个 enumT 存在 unsigned char我已经尝试了这个但是没有走远.
这三种情况的正确功能声明有哪些作用?
我能够想出的最接近的是案例1:
template<typename T>
typename std::enable_if< std::is_enum<T>::value, bool >::type func( T &t, int x)
Run Code Online (Sandbox Code Playgroud)
和案例3是:
template<typename T>
typename std::enable_if< not std::is_enum<T>::value, bool >::type func( T &t, int x)
Run Code Online (Sandbox Code Playgroud)
但是我无法为编译的案例2做些工作.作为一种解决方法,我if在案例3中有一个声明来处理未签名的字符,但这并不理想.
使用标签调度:
namespace details {
template<class T>
bool func( T& t, int x, std::true_type /* is_enum */, std::false_type ) {
}
template<class T>
bool func( T& t, int x, std::false_type, std::true_type /* unsigned char */ ) {
}
template<class T>
bool func( T& t, int x, std::false_type, std::false_type ) {
// neither
}
}
template<class T>
bool func( T& t, int x ) {
return details::func( t, x, std::is_enum<T>{}, std::is_same<unsigned char, T>{} );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1159 次 |
| 最近记录: |