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

edw*_*inc 3 c++ templates variadic-templates

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

    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)

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

lor*_*rro 5

基本上,您想要可选的列表附加。为此,您首先需要添加列表:

template<typename... Ts>
struct list {
    template<typename T>
    using append = list<Ts..., T>;

    template<bool b, typename T>
    using appendIf = std::conditional_t<b, list<Ts..., T>, list<Ts...>>;

    template<template<class...> LT>
    using applyTo = LT<Ts...>;
};
Run Code Online (Sandbox Code Playgroud)

然后您可以从(或您肯定拥有的任何类型)开始list<>,然后在每一步中使用::appendIf<condition, type>并以 结束applyTo<DispatcherT>

using Dispatcher = 
    list<TimerPump>
        ::appendIf<Traits::gui, GuiPump>
        ::appendIf<Traits::network, NetworkPump>
        ::applyTo<DispatcherT>;
Run Code Online (Sandbox Code Playgroud)