gpt4 book ai didi

c++ - 有没有办法检测混合类型和非类型的任意模板类?

转载 作者:行者123 更新时间:2023-12-04 18:03:02 28 4
gpt4 key购买 nike

有没有办法检测一个类是普通类型还是模板类型(元类型)的实例化可能包含非类型参数 ?我想出了这个解决方案:

#include <iostream>

template <template<class...> class>
constexpr bool is_template()
{
return true;
}

template <class>
constexpr bool is_template()
{
return false;
}

struct Foo{};

template<class> struct TemplateFoo{};

template<class, int> struct MixedFoo{};

int main()
{
std::cout << std::boolalpha;
std::cout << is_template<Foo>() << std::endl;
std::cout << is_template<TemplateFoo>() << std::endl;
// std::cout << is_template<MixedFoo>() << std::endl; // fails here
}

但是对于混合非类型和类型的模板,它会失败,比如
template<class, int> struct MixedFoo{};

我想不出任何解决方案,除了我必须在重载中明确指定类型的解决方案。当然,由于组合爆炸,这是不合理的。

相关问题(不是骗子): Is it possible to check for existence of member templates just by an identifier?

最佳答案

不,那里没有。

请注意,模板类本身不是类。它们是类的模板。

关于c++ - 有没有办法检测混合类型和非类型的任意模板类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812789/

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