3

Given the following code:

struct X
{
    explicit X() {}
    X( const X & ) = delete;
    X & operator=( const X & ) = delete;
    X( X && ) = default;
    X & operator=( X && ) = default;
};

int
main()
{
    std::vector<X> t;
    t = { X{} };
    return 0;
}

is it supposed to fail? Until now, I assumed that std::vector would be move-initialized from the initializer list, but at least with g++ 7.4 in C++17, the initialization of the vector is done via the copy constructor.

Markus Mayr
  • 4,038
  • 1
  • 20
  • 42
  • A small note. In this code `{ X{} }` is not a `std::initializer_list`, but a braced initializer list. `std::initializer_list` will appear later, when `vector::operator=(std::initializer_list)` will be chosen during overload resolution. For example, if `std::vector` is replaced with `std::array`, the code will compile and the move assignment operator will be invoked. [Demo](https://godbolt.org/z/jrUSfJ). – Evg Dec 09 '19 at 13:20

0 Answers0