在过去使用SFINAE选择构造函数重载时,我通常使用以下内容:
template <typename T>
class Class {
public:
template <typename U = T, typename std::enable_if<std::is_void<U>::value, int>::type=0>
Class() {
std::cout << "void" << std::endl;
}
template <typename U = T, typename std::enable_if<!std::is_void<U>::value, int>::type=0>
Class() {
std::cout << "not void" << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
但是,我刚刚遇到了这个替代方案:
template <typename U = T, typename std::enable_if<std::is_void<U>::value>::type...>
Class() {
std::cout << "void" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
考虑到以下是非法的......
template <typename U = T, void...> // ERROR!
Class() { }
Run Code Online (Sandbox Code Playgroud)
...上面使用省略号而不是非类型模板参数的替代方法如何工作?