C++/C++ 11 - 可变参数模板的switch语句?

non*_*all 17 templates metaprogramming variadic switch-statement c++11

假设我有一些像这样的结构:

struct MyStruct1 {

    inline void DoSomething() {
        cout << "I'm number one!" << endl;
    }

};

struct MyStruct2 {

    static int DoSomething() {

        cout << "I'm the runner up." << endl;
        return 1;

    }

};

struct MyStruct3 {

    void (*DoSomething)();

    MyStruct3() {
        DoSomething = &InternalFunction;
    }

    static void InternalFunction() {
        cout << "I'm the tricky loser." << endl;
    }

};
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,对于所有三个结构,我可以在该结构的对象上调用DoSomething()并使其工作(尽管每个结构的实现方式不同):

MyStruct1 a;
MyStruct2 b;
MyStruct3 c;

a.DoSomething(); // works, calls Struct1's instance function
b.DoSomething(); // works, calls Struct2's static function, discards return value
c.DoSomething(); // works, calls Struct3's function pointer
Run Code Online (Sandbox Code Playgroud)

现在,假设我将这些结构中的任意选择放入元组中:

tuple<MyStruct2, MyStruct3, MyStruct2, MyStruct1> collection;
Run Code Online (Sandbox Code Playgroud)

我们还要说,我想采用其中一个元素DoSomething(),并根据在运行时确定的索引运行其函数.为此,我可以使用switch语句:

switch(index) {

case 0: get<0>(collection).DoSomething(); break;
case 1: get<1>(collection).DoSomething(); break;
case 2: get<2>(collection).DoSomething(); break;
case 3: get<3>(collection).DoSomething(); break;

}
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但是当需要使用多个不同排列(并且可能比4个元素长得多)的元组时,它会变得非常繁琐,重复且容易出错.如果可以根据可变参数模板中的元素数自动生成switch语句,那将非常方便.伪代码:

template <typename... T>
void DoSomethingByIndex(int index, tuple<T...>& collection) {

    switch(index) {

    STATIC_REPEAT(sizeof...(T), X) {
    case X: get<X>(collection).DoSomething(); break;
    }

    }

}
Run Code Online (Sandbox Code Playgroud)

C++ 11中是否有任何机制可以让我实现这一目标?如果没有,我知道我可以毫无疑问地将一个解决方案与一个模板中的函数指针列表混在一起,但我只是好奇这样的东西是否存在,因为它更适合我的目的.我确信switch语句的编译器生成的跳转列表也比我自己的函数指针解决方案更有效.

Luc*_*ton 17

您可以使用数组来桥接编译时和运行时:(ab)使用可变参数模板静态初始化数组元素,然后使用runtime参数索引到数组中.棘手的部分是为数组找到正确的元素类型.另外,因为我们需要模板在元组索引上是可变的而不是元组元素,所以我将使用我常用的技巧.

template<int... Indices>
struct indices {
    typedef indices<Indices..., sizeof...(Indices)> next;
};

template<int N>
struct build_indices {
    typedef typename build_indices<N - 1>::type::next type;
};

template<>
struct build_indices<0> {
    typedef indices<> type;
};

// No need to be variadic on the tuple elements as we don't care about them
// So I'm using perfect forwarding for the tuple
template<typename Tuple, int... Indices>
void
do_something_by_index(Tuple&& tuple, int index, indices<Indices...>)
{
    using std::get;

    typedef void (*element_type)(Tuple&&);
    static constexpr element_type table[] = {
        [](Tuple&& tuple)
        { get<Indices>(std::forward<Tuple>(tuple)).DoSomething(); }
        ...
    };

    table[index](std::forward<Tuple>(tuple));
}

// Proverbial layer of indirection to get the indices
template<typename Tuple>
void
do_something_by_index(Tuple&& tuple, int index)
{
    typedef typename std::decay<Tuple>::type decay_type;
    constexpr auto tuple_size = std::tuple_size<decay_type>::value;
    typedef typename build_indices<tuple_size>::type indices_type;

    do_something_by_index(std::forward<Tuple>(tuple), index, indices_type{});
}
Run Code Online (Sandbox Code Playgroud)

  • 最好使用包装扩展:-) (4认同)