简体   繁体   English

如果我的班级成员的类型与类型匹配,那么如何进行编译时间type_check并仅编译班级的部分?

[英]How to do a compile time type_check and only compile parts of my class if my class members' type matches the types?

template<class T = int>
struct v2 {
  T x;
  // this is the part
  template<class T, std::enable_if?>
  v2& operator++(int n) {}
};

I would like to enable it so ++v2 only compiles when it's an integer (or a long), and doesn't if it's anything else. 我想启用它,因此++v2仅在它是整数(或长整数)时才进行编译,而在其他情况下则不进行编译。

You need to partially specialize 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);
};

Alternatively, common functionality can be put in another class used as base of v2 . 或者,可以将通用功能放在用作v2基础的另一个类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM