iterating over variadic template's type parameters

前端 未结 2 1246
忘掉有多难
忘掉有多难 2020-12-14 18:19

I have a function template like this:

template 
do_something()
{
  // i\'d like to do something to each A::var, where var has static storag         


        
相关标签:
2条回答
  • 2020-12-14 18:37

    What Xeo said. To create a context for pack expansion I used the argument list of a function that does nothing (dummy):

    #include <iostream>
    #include <initializer_list>
    
    template<class...A>
    void dummy(A&&...)
    {
    }
    
    template <class ...A>
    void do_something()
    {
        dummy( (A::var = 1)... ); // set each var to 1
    
        // alternatively, we can use a lambda:
    
        [](...){ }((A::var = 1)...);
    
        // or std::initializer list, with guaranteed left-to-right
        // order of evaluation and associated side effects
    
        auto list = {(A::var = 1)...};
    }
    
    struct S1 { static int var; }; int S1::var = 0;
    struct S2 { static int var; }; int S2::var = 0;
    struct S3 { static int var; }; int S3::var = 0;
    
    int main()
    {
        do_something<S1,S2,S3>();
        std::cout << S1::var << S2::var << S3::var;
    }
    

    This program prints 111.

    0 讨论(0)
  • 2020-12-14 18:47

    As an example, suppose you want to display each A::var. I see three ways to acomplish this as the code below illustrates.

    Regarding option 2, notice that the order in which the elements are processed is not specified by the standard.

    #include <iostream>
    #include <initializer_list>
    
    template <int i>
    struct Int {
        static const int var = i;
    };
    
    template <typename T>
    void do_something(std::initializer_list<T> list) {
        for (auto i : list)
            std::cout << i << std::endl;
    }
    
    template <class... A>
    void expand(A&&...) {
    }
    
    template <class... A>
    void do_something() {
    
        // 1st option:
        do_something({ A::var... });
    
        // 2nd option:
        expand((std::cout << A::var << std::endl)...);
    
        // 3rd option:
        {
            int x[] = { (std::cout << A::var << std::endl, 0)... };
            (void) x;
        }
    }
    
    int main() {
        do_something<Int<1>, Int<2>, Int<3>>();
    }
    
    0 讨论(0)
提交回复
热议问题