gpt4 book ai didi

c++ - 如何检查两种类型是否来自同一个模板类

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:16 28 4
gpt4 key购买 nike

<分区>

我想检查两种类型是否相同,但不管它们的模板参数如何。像这样:

template<class T>
class A {};
class B {};

int main() {
cout << std::is_same_template<A<int>, A<string>>::value << endl; // true
cout << std::is_same_template<A<int>, B>::value << endl; // false
}

我知道 std::is_same 用于检查两种类型是否完全匹配。

我需要这个的原因:我有一个可以用任何类型调用的模板化方法,但我想禁止使用 A 类型(它是模板化的)调用它,可能是通过使用 static_assert。如果 A 没有模板化,我相信可以使用 std::is_same 轻松完成,但是现在,我遇到了一个问题...

编辑:我可以为一些常见的 T 手动排除 A,使用,我正在寻找一种方法来为所有 T 执行此操作:

static_assert(!std::is_same<parameter_type, A<int>>::value, "Cannot use this function with type A<T>");
static_assert(!std::is_same<parameter_type, A<double>>::value, "Cannot use this function with type A<T>");
static_assert(!std::is_same<parameter_type, A<bool>>::value, "Cannot use this function with type A<T>");

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com