Edd*_*chs 2 c++ templates type-traits
我有以下代码:
#include <iostream>
#include <type_traits>
using namespace std;
template<typename T, int N>
class A {
public:
static constexpr int n = N;
};
template<typename T, typename X, int N>
class B {
public:
static constexpr int x = N;
};
template<typename T>
void test(T) {
if constexpr (std::is_same_v<T, A>) {
int a = T::n;
} else if constexpr (std::is_same_v<T, B>) {
int a = T::x;
}
cout << "a";
};
int main()
{
A<int, 2> a;
test(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译它会产生以下错误:
error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Tp, class _Up> constexpr const bool std::is_same_v<_Tp, _Up>’
20 | if constexpr (std::is_same_v<T, A>) {
| ~~~~~^~~~~~~~~~~~~~~
note: expected a type, got ‘A’
Run Code Online (Sandbox Code Playgroud)
问题是我不能在这里使用正确的类型(如 A),因为我不知道模板参数是什么或有多少。基本上我想将任何类型的 A 类与任何模板参数相匹配。
您需要为此编写自己的特征:
template<typename>
struct is_specialization_of_A : std::false_type {};
template<typename T, int N>
struct is_specialization_of_A<A<T,N>> : std::true_type {};
template<typename T>
inline constexpr auto is_specialization_of_A_v = is_specialization_of_A<T>::value;
Run Code Online (Sandbox Code Playgroud)
并且类似地对于B。
您也可以将模板作为特征的模板模板参数,这样您只需要一个特征定义,但只有模板的模板参数类别匹配时才有效。这不是这里的情况(Ahas <type, non-type>, while Bhas <type, type, non-type>)。
template<typename, template<typename, auto> class>
struct is_specialization_of : std::false_type {};
template<template<typename, auto> class Tmpl, typename T, auto N>
struct is_specialization_of<Tmpl<T, N>, Tmpl> : std::true_type {};
template<typename T, template<typename, auto> class Tmpl>
inline constexpr auto is_specialization_of_v = is_specialization_of<Tmpl, T>::value;
Run Code Online (Sandbox Code Playgroud)
这可以用作is_specialization_of_v<A, T>,但不能用作is_specialization_of_v<B, T>。
| 归档时间: |
|
| 查看次数: |
698 次 |
| 最近记录: |