Why was the destination transfer std :: initializer_list not blocked?

Clearly, std :: initializer_list is not an actual container. The standard clearly defines what you can and cannot do with std :: initializer_list.

But why does the language retain the ability to do stupid things, for example, assign a temporary std :: initializer_list to another , when it could be easily blocked using = delete the move destination operator in std :: initializer_list?

Here is an example of the code that compiles:

void foo(std::initializer_list<int> v) {
    std::cout << *v.begin() << std::endl;
}

int main() {
    int a = 1, b = 2, c = 3;
    auto val = {a, b, c}; // ok, extending the lifetime of {a, b, c}
    foo(val); // prints ok
    int i = 7;
    val = {i}; // doesn't handle well assignment of temporary
    foo(val); // prints garbage...
}

Exit , courtesy of @nwp:

1
-365092848
+4
source share

Source: https://habr.com/ru/post/1692983/


All Articles