Use brace-enclosed initializer lists in a variadic template?

后端 未结 2 695
无人及你
无人及你 2021-01-18 07:43

I\'m trying to use brace-enclosed initializer lists in a variadic template function, but the compiler complains... am I asking too much or did I do something wrong?

2条回答
  •  终归单人心
    2021-01-18 08:15

    This is a rough attempt at what you want...

    #include 
    #include 
    
    struct Bracy {
        Bracy(int x, int y) {}
    };
    
    struct Test {
        void consumeOne(std::initializer_list) { std::cout << "initializer list version (Bracy?)\n"; /* Bracy? */}
    
        void consumeOne(int) { std::cout << "int version\n"; }
    
        template
        void consume(T t) { consumeOne(t); }
    
        template
        void consume(T first, Args ... args) {
            consumeOne(first);
            consume(args...);
        }
    
        template
        Test(Args ... args) {
            consume(args...);
        }
    };
    
    int
    main(int argc, char** argv) {
        Test(1, std::initializer_list{1,2}, 2, 3, std::initializer_list{1,2});
        return 0;
    }
    
    output: int version
            initializer list version (Bracy?)
            int version
            int version
            initializer list version (Bracy?)
    

提交回复
热议问题