jeudi 27 mai 2021

C++: Initialization (copy-list / direct-list)

I would consider myself rather proficient in C++. However, on cppreference (https://en.cppreference.com/w/cpp/language/converting_constructor), I read something today that really bummed me.

I had understood that, when it comes to initialization, the cases below are exactly equivalent. However, in cppreference, depending of the case, sometimes direct-list-initialization is used and sometimes copy-list-initialization is used.

How are they different under the hood?

class A
{
public:
    B b;
    C c;
// Constructors, copy-constructors, move-constructors, operator=()...
}
int main()
{
    A a1(b1,c1); // 1.
    A a2({b1,c1}); // 2.
    A a3{b1,c1}; // 3.
    A a4 = {b1,c1}; // 4.
}

Also, what is the intrinsic variable type of a list {var1,var2,var3} without it being casted to whatever object they're used to initialize it? Something like std::initialization_list?

Aucun commentaire:

Enregistrer un commentaire