Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何进行编译时类型检查,如果我的类成员';类型与类型匹配? 模板 结构v2{ tx; //这就是部分 样板 v2&运算符++(int n){} };_C++_Templates_Compilation - Fatal编程技术网

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);
};