Is double-braced scalar initialization allowed by the C++ standard?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
33
down vote

favorite
2












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'











share|improve this question



























    up vote
    33
    down vote

    favorite
    2












    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'











    share|improve this question

























      up vote
      33
      down vote

      favorite
      2









      up vote
      33
      down vote

      favorite
      2






      2





      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'











      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 29 at 13:00









      Boann

      35.7k1184116




      35.7k1184116










      asked Aug 29 at 10:34









      Igor

      36939




      36939






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          34
          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





          share|improve this answer
















          • 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










          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          34
          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





          share|improve this answer
















          • 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














          up vote
          34
          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





          share|improve this answer
















          • 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












          up vote
          34
          down vote



          accepted







          up vote
          34
          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 29 at 10:48









          Rakete1111

          32.7k975110




          32.7k975110







          • 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




            +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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?