In C++11, are the aggregates allowed to be copied with curly-braces syntax? I have the following code:
struct s
{
int x;
};
template<class T>
struct holder
{
template<class A>
holder(A&& x) : t{x} {}
T t;
};
Each one of the statements below works.
auto s1 = s{1};
auto s2(s1);
auto s3{s1}; ///NOTE : this works!
However, the second statement below raises the error cannot convert 's' to 'int' in initialization
.
holder<s> h{5};
holder<s> h1{s{5}};
I am using gcc 4.8.2. Why do I get this error?
First of all this code
is compiled successfuly.
The invalid statement is
The problem is that in fact you are trying to execute
However the compiler is unable to convert an object of type s to an object of type int (that to initialize s1.x) when it tries to make assignment
In C++11, when a type
T
is an aggregate type, initialisation using{ ... }
performs aggregate initialisation. Aggregate initialisation always initialises the members ofT
, notT
itself.Although this is exactly what the standard requires, this is unwanted, which is why in a future standard, the rule will likely be changed to make a special exception for initialisation from the same type. This is core language issue 1467.
Until that time, unfortunately, the error you are getting is entirely correct, and you will have to work around it.