C++ 如何进行编译时类型检查,如果我的类成员';类型与类型匹配? 模板 结构v2{ tx; //这就是部分 样板 v2&运算符++(int n){} };
我想启用它,这样C++ 如何进行编译时类型检查,如果我的类成员';类型与类型匹配? 模板 结构v2{ tx; //这就是部分 样板 v2&运算符++(int n){} };,c++,templates,compilation,C++,Templates,Compilation,我想启用它,这样++v2只在它是整数(或长)时才编译,如果它是其他类型,则不编译。您需要部分专门化v2: template<class T = int> struct v2 { T x; // this is the part template<class T, std::enable_if?> v2& operator++(int n) {} }; 模板 结构v2{ tx; }; 样板 结构v2{ tx; v2&运算符++(int); };
++v2
只在它是整数(或长)时才编译,如果它是其他类型,则不编译。您需要部分专门化v2
:
template<class T = int>
struct v2 {
T x;
// this is the part
template<class T, std::enable_if?>
v2& operator++(int n) {}
};
模板
结构v2{
tx;
};
样板
结构v2{
tx;
v2&运算符++(int);
};
或者,公共功能可以放在另一个类中,用作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);
};