template<class T = int>
struct v2 {
T x;
// this is the part
template<class T, std::enable_if?>
v2& operator++(int n) {}
};
我想启用它,这样 ++v2
只在它是整数(或长整数)时编译,如果是其他任何东西则不编译。
最佳答案
您需要部分特化 v2
:
template<class T = int, typename = void>
struct v2 {
T x;
};
template<class T>
struct v2<T, std::enable_if_t<std::is_same_v<T, int> || std::is_same_v<T, long>>> {
T x;
v2& operator++(int);
};
或者,通用功能可以放在另一个类中用作 v2
的基础。
关于c++ - 如果我的类成员的类型与类型匹配,如何进行编译时 type_check 并只编译我的类的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50424720/