Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializer_list<T> assignment operator requirement on T

I am wondering if an initializer_list<T> requires that T has an assignment operator. The following

struct Foo
{
    Foo& operator=( const Foo& ) = delete;
};

std::vector<Foo> f = { Foo( ), Foo( ) };

compiles on clang 3.4.2 but fails on Visual Studo 2013 with a "error C2280: 'Foo &Foo::operator =(const Foo &)' : attempting to reference a deleted function". I am assuming clang is correct here but wanted to check that there is no requirement for T to to be assignable.

like image 208
goneskiing Avatar asked Sep 03 '14 11:09

goneskiing


1 Answers

std::initializer_list<T> does not require T to be assignable in any way, because copying std::initializer_list objects is shallow - it does not copy the underlying data.

You're getting the error because MSVC 2013 (and below) does not support automatic generation of move constructors and move assignment operators. So the error actually comes from std::vector trying to copy from the initializer list, not from the initializer list itself.

It's still a question why the error is triggered, since the std::vector constructor taking std::initializer_list only requires the type to be EmplaceConstructible and possibly MoveInsertable, neither of which uses assignments.

like image 99
Angew is no longer proud of SO Avatar answered Nov 09 '22 08:11

Angew is no longer proud of SO