在C++模板实例化期间获取原始结构/类名

Tad*_*owo 6 c++ static-assert template-meta-programming

template<typename T> struct S {};
template<typename T> struct R {};

int main() {
  typedef S<double> s1;
  typedef S<int> s2;
  typedef R<int> s3;
  static_assert(xxx<s1, s2>::value,
                "No, assertion must not be raised");
  static_assert(xxx<s2, s3>::value,
                "Yes, assertion must be raised");
}
Run Code Online (Sandbox Code Playgroud)

所以,我希望在编译时返回false xxx<s1, s2>::value而xxx<s2, s3>::value返回true.

在C++中是不是存在xxx?或者,在C++中理论上是否存在xxx,但可能还没有人做过呢?

Dan*_*our 5

使用两个使用模板模板参数的特化来执行此"匹配":

template<
  typename T,
  typename V>
struct xxx;

template<
 template <class> class A,
 template <class> class B,
 typename X,
 typename Y>
struct xxx<A<X>, B<Y>> {
  static constexpr const int value = false;
};


template<
 template <class> class U,
 typename X,
 typename Y>
struct xxx<U<X>, U<Y>> {
  static constexpr const int value = true;
};
Run Code Online (Sandbox Code Playgroud)

将您的代码放在ideone上

注意:要使它成为真正的类型特征,您不应value手动设置,而应从std::integral_constant(std::true_type或std::false_type)派生.以上只是我在手机上做的快速模型.