samedi 29 août 2015

c++11 constructor with variadic universal references and copy constructor

How to declare copy constructor, if we have constructor with universal reference arguments, also?

http://ift.tt/1LBY5sE

struct Record{
    template<class ...Refs>
    explicit Record(Refs&&... refs){
        cout << "param ctr" << endl;
    }

    Record(const Record& other){     // never called
        cout << "copy ctr" << endl;
    }

    Record(Record&& other){         // never called
        cout << "move ctr" << endl;
    }    
};

int main() {
    Record rec("Hello");    
    Record rec2(rec);  // do "param ctr"

    return 0;
}

According to this constructor list of std::tuple http://ift.tt/1NNO6CK [look case 3 and 8] this problem somehow solved in standard library... But I can't get through stl's code.


P.S. Question somewhat related to C++ universal reference in constructor and return value optimization (rvo)

P.P.S. For now, I just added additional first param Record(call_constructor, Refs&&... refs) for really EXPLICIT call. And I can manually detect if we have only one param and if it is Record, and than redirect call to copy ctr/param ctr, but.... I can't believe there is no standard way for this...

Aucun commentaire:

Enregistrer un commentaire