mardi 6 juillet 2021

Is sharing data with initializer list out-scoped valid in standard?

lst2 = lst Copying or assigning an initializer_list does not copy the elements in the list. After the copy, the original and the copy share the elements.

According to C++ Primer Table 6.1, assign one initializer list will share the data, but what if the initializer list share with an other out-scoped, for example, the code below is a part of a function

std::initializer_list<int> lst1;
{
    std::initializer_list<int> lst2 = {1, 2, 3};
    lst1 = lst2;
}

it seems initializer lists may share the literal's data, but what is the lifetime of the array literal specified in the standard? is this code safe?

Aucun commentaire:

Enregistrer un commentaire