Inserting a variadic argument list into a vector?

后端 未结 6 1079
别那么骄傲
别那么骄傲 2020-12-09 02:44

Forgive me if this has been answered already, as I couldn\'t find it...

Basically I have an object that needs to take a variadic argument list in it\'s constructor

6条回答
  •  遥遥无期
    2020-12-09 03:28

    The best thing would be to use an initializer list

    #include 
    #include 
    class GenericNode {
    public:
        GenericNode(std::initializer_list inputs) 
            :inputs_(inputs) {} //well that's easy
    private:
        std::vector inputs_;
    };
    int main() {
        GenericNode* ptr;
        GenericNode node{ptr, ptr, ptr, ptr};
    } //compilation at http://stacked-crooked.com/view?id=88ebac6a4490915fc4bc608765ba2b6c
    

    The closest to what you already have, using C++11 is to use the vector's initializer_list:

        template
        GenericNode(Ts... inputs) 
            :inputs_{inputs...} {} //well that's easy too
        //compilation at http://stacked-crooked.com/view?id=2f7514b33401c51d33677bbff358f8ae
    

    And here's a C++11 version with no initializer_lists at all. It's ugly, and complicated, and requires features missing from many compilers. Use the initializer list

    template
    using Alias = T;
    
    class GenericNode {
    public:
        template
        GenericNode(Ts... inputs) { //SFINAE might be appropriate
             using ptr = GenericNode*;
             Alias{( //first part of magic unpacker
                 inputs_.push_back(ptr(inputs))
                 ,'0')...,'0'}; //second part of magic unpacker
        }
    private:
        std::vector inputs_;
    };
    int main() {
        GenericNode* ptr;
        GenericNode node(ptr, ptr, ptr, ptr);
    } //compilation at http://stacked-crooked.com/view?id=57c533692166fb222adf5f837891e1f9
    //thanks to R. Martinho Fernandes for helping me get it to compile
    

    Unrelated to everything, I don't know if those are owning pointers or not. If they are, use std::unique_ptr instead.

提交回复
热议问题