What is the rationale behind allowing the initialization of scalars with braces?
int
is POD. So the brace initialization is allowed in case of int
(and for all build-in types), as it makes the initialization-syntax consistent with other PODs.
Also, I guess whatever rationale behind C++11 Uniform Initialization Syntax are, are also (partially) applicable to this syntax allowed by C++03. It is just C++03 didn't extend this to include non-pod types such as the standard containers.
I can see one place where this initialization is helpful in C++03.
template<typename T>
void f()
{
T obj = { size() } ; //T is POD: built-in type or pod-struct
//code
}
Now this can be instantiated with struct
which begins with a suitable member, as well as any arithmetic type:
struct header
{
size_t size; //it is the first member
//...
};
f<header>(); //body becomes : header obj = { size(); }; which is fine
f<size_t>(); //body becomes : size_t obj = { size(); }; which is fine
Also note that POD, whether struct or built-in types, can also be initialized uniformly as:
header h = header(); //value-initialized
int i = int(); //value-initialized
So I believe one reason is consistency!
-pedantic
? Maybe it is just an compiler extension. – Petronillegcc -pedantic
, and it still works. – Whortleberry