小编edw*_*inc的帖子

确定可变参数模板参数是编译时

假设我想定义一个依赖于某些类型的类型:

    struct TimerPump{};
    struct GuiPump{};
    struct NetworkPump{};
    
    template<class... Pumps>
    class DispatcherT{};
    
    using Dispatcher = DispatcherT< TimerPump, GuiPump, NetworkPump >;
Run Code Online (Sandbox Code Playgroud)

我想让 GUI 和网络泵成为可选的。可能需要其中之一,或者两者都需要,或者都不需要。我可以编写一个预处理器宏:

using Dispatcher = DispatcherT< TimerPump
#ifdef GUI_ENABLED
                                , GuiPump
#endif
#ifdef NETWORK_ENABLED
                                , NetworkPump 
#endif
>;
Run Code Online (Sandbox Code Playgroud)

但我正在寻找一种通过特征来控制这些论点的方法

struct Traits
{
    static constexpr bool gui = true;
    static constexpr bool network = false;
};

using Dispatcher = DispatcherT< TimerPump
                                , Traits::gui ? GuiPump : null     <--- need help here
                                , Traits::network ? NetworkPump : null
>;
Run Code Online (Sandbox Code Playgroud)

有没有一种巧妙的方法来确定传递给采用可变参数的模板的参数?

c++ templates variadic-templates

3
推荐指数
1
解决办法
94
查看次数

标签 统计

c++ ×1

templates ×1

variadic-templates ×1