gpt4 book ai didi

c++ - 在 C++ 中创建新的类型特征

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

我想使用类型特征来检测类是否是我的库中的有效执行策略。

// execution.h
namespace MyLib::execution {
struct policy1 { ... };
struct policy2 { ... };
...
}

我通常将我所有的类型特征放在一个文件中:

// traits.h
namespace MyLib::execution { // forward declarations
struct policy1;
struct policy2;
...
}

namespace MyLib {
template <typename T> struct is_execution_policy : std::false_type {};

template <> struct is_execution_policy<MyLib::execution::policy1> : std::true_type {};
template <> struct is_execution_policy<MyLib::execution::policy2> : std::true_type {};
...
}

它有效,但我觉得它很尴尬。首先,我不喜欢 traits.h 中的前向声明。我基本上必须重新申报一切。有没有更好的方法来处理我的特质?我可以将特化移动到 execution.h 并只将主模板留在 traits.h 中吗?我以后会遇到这两种方法的问题吗?

最佳答案

Could I move the specializations to execution.h and only leave the primary template in traits.h?

是的。


Would I run into issues later with either approach?

假设 execution.h 依赖于 traits.h,我想不出有什么特别的问题。

关于c++ - 在 C++ 中创建新的类型特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195965/

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