C++ overloading operator comma for variadic arguments

前端 未结 4 599
终归单人心
终归单人心 2021-01-02 17:52

is it possible to construct variadic arguments for function by overloading operator comma of the argument? i want to see an example how to do so.., maybe something like this

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 18:36

    Maybe something like this:

    class MyArgList {
    public:   
         typedef std::list ManyList;
    
         template 
         MyArgList& operator, (const T& val) {
            elems.push_back(val);
            return *this; 
         }
    
         ManyList::iterator begin() {return elems.begin();}
           ...
    
    private:
         ManyList elems;
    };
    

    Usage would be:

    void foo(MyArgList& list);
    foo((myArgList(),1,2,3,4,5));
    

提交回复
热议问题