簡體   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) {}
};

我想啟用它,因此++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基礎的另一個類中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM