Is double-braced scalar initialization allowed by the C++ standard?
Clash Royale CLAN TAG#URR8PPP
up vote
31
down vote
favorite
I have the following code:
int x = ;
Is this syntax valid according to the C++ standard? (I'm interested in C++11 and later.)
When using the latest compilers there is no problem, however in some older ones (e.g. GCC 4.8.5) it gives following error:
error: braces around scalar initializer for type 'int'
c++ c++11 language-lawyer
add a comment |Â
up vote
31
down vote
favorite
I have the following code:
int x = ;
Is this syntax valid according to the C++ standard? (I'm interested in C++11 and later.)
When using the latest compilers there is no problem, however in some older ones (e.g. GCC 4.8.5) it gives following error:
error: braces around scalar initializer for type 'int'
c++ c++11 language-lawyer
add a comment |Â
up vote
31
down vote
favorite
up vote
31
down vote
favorite
I have the following code:
int x = ;
Is this syntax valid according to the C++ standard? (I'm interested in C++11 and later.)
When using the latest compilers there is no problem, however in some older ones (e.g. GCC 4.8.5) it gives following error:
error: braces around scalar initializer for type 'int'
c++ c++11 language-lawyer
I have the following code:
int x = ;
Is this syntax valid according to the C++ standard? (I'm interested in C++11 and later.)
When using the latest compilers there is no problem, however in some older ones (e.g. GCC 4.8.5) it gives following error:
error: braces around scalar initializer for type 'int'
c++ c++11 language-lawyer
edited Aug 29 at 13:00
Boann
35.6k1184116
35.6k1184116
asked Aug 29 at 10:34
Igor
35939
35939
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
33
down vote
accepted
This is ill-formed. gcc is wrong to accept it and clang seems to allow it as an extension, as it warns about it.
I'm going to quote the latest draft, but it doesn't make a difference. List initialization works as follows as per [dcl.init.list], where T is int
in this case:
- If the initializer list is a designated initializer list, [...] => it's not
- If T is an aggregate class [...] => it's not
- If T is a character array [...] => it's not.
- If T is an aggregate [...] => it's not (only arrays and classes are aggregates)
- If the initializer list has no elements [...] => it doesn't
- If T is a specialization of std::initializer_list [...] => it's not
- If T is a class type [...] => it's not
- If T is an enumeration with fixed underlying type [...] => it's not
- If the initializer list has a single element of type E [...] => a braced initializer list has no type, so no
- If T is a reference type [...] => it isn't
- If the initializer list has no elements [...] => it doesn't
- Otherwise the program is ill-formed
4
+1 and see also core language issue 1501 which follows the same logic as this answer to explicitly say that it's invalid.
â hvd
Aug 29 at 11:51
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
33
down vote
accepted
This is ill-formed. gcc is wrong to accept it and clang seems to allow it as an extension, as it warns about it.
I'm going to quote the latest draft, but it doesn't make a difference. List initialization works as follows as per [dcl.init.list], where T is int
in this case:
- If the initializer list is a designated initializer list, [...] => it's not
- If T is an aggregate class [...] => it's not
- If T is a character array [...] => it's not.
- If T is an aggregate [...] => it's not (only arrays and classes are aggregates)
- If the initializer list has no elements [...] => it doesn't
- If T is a specialization of std::initializer_list [...] => it's not
- If T is a class type [...] => it's not
- If T is an enumeration with fixed underlying type [...] => it's not
- If the initializer list has a single element of type E [...] => a braced initializer list has no type, so no
- If T is a reference type [...] => it isn't
- If the initializer list has no elements [...] => it doesn't
- Otherwise the program is ill-formed
4
+1 and see also core language issue 1501 which follows the same logic as this answer to explicitly say that it's invalid.
â hvd
Aug 29 at 11:51
add a comment |Â
up vote
33
down vote
accepted
This is ill-formed. gcc is wrong to accept it and clang seems to allow it as an extension, as it warns about it.
I'm going to quote the latest draft, but it doesn't make a difference. List initialization works as follows as per [dcl.init.list], where T is int
in this case:
- If the initializer list is a designated initializer list, [...] => it's not
- If T is an aggregate class [...] => it's not
- If T is a character array [...] => it's not.
- If T is an aggregate [...] => it's not (only arrays and classes are aggregates)
- If the initializer list has no elements [...] => it doesn't
- If T is a specialization of std::initializer_list [...] => it's not
- If T is a class type [...] => it's not
- If T is an enumeration with fixed underlying type [...] => it's not
- If the initializer list has a single element of type E [...] => a braced initializer list has no type, so no
- If T is a reference type [...] => it isn't
- If the initializer list has no elements [...] => it doesn't
- Otherwise the program is ill-formed
4
+1 and see also core language issue 1501 which follows the same logic as this answer to explicitly say that it's invalid.
â hvd
Aug 29 at 11:51
add a comment |Â
up vote
33
down vote
accepted
up vote
33
down vote
accepted
This is ill-formed. gcc is wrong to accept it and clang seems to allow it as an extension, as it warns about it.
I'm going to quote the latest draft, but it doesn't make a difference. List initialization works as follows as per [dcl.init.list], where T is int
in this case:
- If the initializer list is a designated initializer list, [...] => it's not
- If T is an aggregate class [...] => it's not
- If T is a character array [...] => it's not.
- If T is an aggregate [...] => it's not (only arrays and classes are aggregates)
- If the initializer list has no elements [...] => it doesn't
- If T is a specialization of std::initializer_list [...] => it's not
- If T is a class type [...] => it's not
- If T is an enumeration with fixed underlying type [...] => it's not
- If the initializer list has a single element of type E [...] => a braced initializer list has no type, so no
- If T is a reference type [...] => it isn't
- If the initializer list has no elements [...] => it doesn't
- Otherwise the program is ill-formed
This is ill-formed. gcc is wrong to accept it and clang seems to allow it as an extension, as it warns about it.
I'm going to quote the latest draft, but it doesn't make a difference. List initialization works as follows as per [dcl.init.list], where T is int
in this case:
- If the initializer list is a designated initializer list, [...] => it's not
- If T is an aggregate class [...] => it's not
- If T is a character array [...] => it's not.
- If T is an aggregate [...] => it's not (only arrays and classes are aggregates)
- If the initializer list has no elements [...] => it doesn't
- If T is a specialization of std::initializer_list [...] => it's not
- If T is a class type [...] => it's not
- If T is an enumeration with fixed underlying type [...] => it's not
- If the initializer list has a single element of type E [...] => a braced initializer list has no type, so no
- If T is a reference type [...] => it isn't
- If the initializer list has no elements [...] => it doesn't
- Otherwise the program is ill-formed
answered Aug 29 at 10:48
Rakete1111
31.9k973107
31.9k973107
4
+1 and see also core language issue 1501 which follows the same logic as this answer to explicitly say that it's invalid.
â hvd
Aug 29 at 11:51
add a comment |Â
4
+1 and see also core language issue 1501 which follows the same logic as this answer to explicitly say that it's invalid.
â hvd
Aug 29 at 11:51
4
4
+1 and see also core language issue 1501 which follows the same logic as this answer to explicitly say that it's invalid.
â hvd
Aug 29 at 11:51
+1 and see also core language issue 1501 which follows the same logic as this answer to explicitly say that it's invalid.
â hvd
Aug 29 at 11:51
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52075339%2fis-double-braced-scalar-initialization-allowed-by-the-c-standard%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password