gpt4 book ai didi

c++ - 模板化类型的模板特化

转载 作者:行者123 更新时间:2023-11-30 01:19:36 29 4
gpt4 key购买 nike

我有一个包含类型特征的结构:

template<typename T> struct x_trait { static const bool has_x = true; };

这对所有类型都是正确的,但对特定模板类型除外。对于特定的模板类型,我想更改特征:

template<> struct x_trait<tt_type<int>> { static const bool has_x = false; };

到目前为止,还不错。但是 tt_type 本身采用不同的模板参数。有没有办法为所有模板化的 tt_type 设置 x_trait?现在我唯一的出路是列出所有类型:

template<> struct x_trait<tt_type<char>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<short>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<int>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<long>> { static const bool has_x = false; };

最佳答案

您可以为 tt_type 模板的所有特化部分特化 x_trait 模板:

template<typename T> 
struct x_trait<tt_type<T>> { static const bool has_x = false; };

关于c++ - 模板化类型的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680407/

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