Why do scoped enums allow use of | operator when initializing using previously assigned values?Why do you use typedef when declaring an enum in C++?Why use the Bitwise-Shift operator for values in a C enum definition?Is it possible to assign numeric value to an enum in Java?How to overload |= operator on scoped enum?Why does Java allow null value to be assigned to an EnumThe behavior of value-initializing an enumIs it allowed for an enum to have an unlisted value?`type *var = (int)0`, legal or not?In C++14, in which scope are unscoped enumerators of redeclared enumerations declared?Implicit conversion of 0 to enums

Can attackers change the public key of certificate during the SSL handshake

A verb for when some rights are not violated?

Why do scoped enums allow use of | operator when initializing using previously assigned values?

I was contacted by a private bank overseas to get my inheritance

How do I get the =LEFT function in excel, to also take the number zero as the first number?

Why do proponents of guns oppose gun competency tests?

New workplace asking for bank pin and account number

What filaments allow air to pass but not water vapor?

Why does putting a dot after the URL remove login information?

How and where to get you research work assessed for PhD?

Generate a random point outside a given rectangle within a map

If I build a custom theme, will it update?

Which genus do I use for neutral expressions in German?

Not been paid even after reminding the Treasurer; what should I do?

Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?

How to realistically deal with a shield user?

How many years before enough atoms of your body are replaced to survive the sudden disappearance of the original body’s atoms?

List: Behavioural characteristics of key Ito processes used in finance

What is the probability of a biased coin coming up heads given that a liar is claiming that the coin came up heads?

Does a 4 bladed prop have almost twice the thrust of a 2 bladed prop?

Is it double speak?

Tile the chessboard with four-colored triominoes

Make a living as a math programming freelancer?

How to check a file was encrypted (really & correctly)



Why do scoped enums allow use of | operator when initializing using previously assigned values?


Why do you use typedef when declaring an enum in C++?Why use the Bitwise-Shift operator for values in a C enum definition?Is it possible to assign numeric value to an enum in Java?How to overload |= operator on scoped enum?Why does Java allow null value to be assigned to an EnumThe behavior of value-initializing an enumIs it allowed for an enum to have an unlisted value?`type *var = (int)0`, legal or not?In C++14, in which scope are unscoped enumerators of redeclared enumerations declared?Implicit conversion of 0 to enums






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















I'm converting unscoped enumerations to scoped enumerations, and have run across a puzzle.



Stroustroup, C++ Programming Language, 4th edition, Section 8.4.1, documents that scoped enum classes are not implicitly converted to integral types, and provides code for operators | and & as an example of how to use static_cast to work around that.



Shouldn't the following initialization using the | operator on previously defined enum values be illegal?



enum class FileCopy CurrentHDU ;

int main()

std::cout << static_cast<int>( FileCopy::AllHDUs) << "n";



I've tested this on Wandbox using both clang & gcc HEAD with --pedantic-errors, and it compiles and returns the expected output, 7. That's not to say it's legal, just that it seems to be accepted by the compilers.



Is this explicitly documented behavior? I've been unable to parse the documentation in a way that describes this behavior.










share|improve this question
































    7















    I'm converting unscoped enumerations to scoped enumerations, and have run across a puzzle.



    Stroustroup, C++ Programming Language, 4th edition, Section 8.4.1, documents that scoped enum classes are not implicitly converted to integral types, and provides code for operators | and & as an example of how to use static_cast to work around that.



    Shouldn't the following initialization using the | operator on previously defined enum values be illegal?



    enum class FileCopy CurrentHDU ;

    int main()

    std::cout << static_cast<int>( FileCopy::AllHDUs) << "n";



    I've tested this on Wandbox using both clang & gcc HEAD with --pedantic-errors, and it compiles and returns the expected output, 7. That's not to say it's legal, just that it seems to be accepted by the compilers.



    Is this explicitly documented behavior? I've been unable to parse the documentation in a way that describes this behavior.










    share|improve this question




























      7












      7








      7


      1






      I'm converting unscoped enumerations to scoped enumerations, and have run across a puzzle.



      Stroustroup, C++ Programming Language, 4th edition, Section 8.4.1, documents that scoped enum classes are not implicitly converted to integral types, and provides code for operators | and & as an example of how to use static_cast to work around that.



      Shouldn't the following initialization using the | operator on previously defined enum values be illegal?



      enum class FileCopy CurrentHDU ;

      int main()

      std::cout << static_cast<int>( FileCopy::AllHDUs) << "n";



      I've tested this on Wandbox using both clang & gcc HEAD with --pedantic-errors, and it compiles and returns the expected output, 7. That's not to say it's legal, just that it seems to be accepted by the compilers.



      Is this explicitly documented behavior? I've been unable to parse the documentation in a way that describes this behavior.










      share|improve this question
















      I'm converting unscoped enumerations to scoped enumerations, and have run across a puzzle.



      Stroustroup, C++ Programming Language, 4th edition, Section 8.4.1, documents that scoped enum classes are not implicitly converted to integral types, and provides code for operators | and & as an example of how to use static_cast to work around that.



      Shouldn't the following initialization using the | operator on previously defined enum values be illegal?



      enum class FileCopy CurrentHDU ;

      int main()

      std::cout << static_cast<int>( FileCopy::AllHDUs) << "n";



      I've tested this on Wandbox using both clang & gcc HEAD with --pedantic-errors, and it compiles and returns the expected output, 7. That's not to say it's legal, just that it seems to be accepted by the compilers.



      Is this explicitly documented behavior? I've been unable to parse the documentation in a way that describes this behavior.







      c++ enums language-lawyer bitwise-operators ienumerator






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 5 hours ago









      Boann

      38.6k13 gold badges93 silver badges123 bronze badges




      38.6k13 gold badges93 silver badges123 bronze badges










      asked 8 hours ago









      Diab JeriusDiab Jerius

      1,6188 silver badges14 bronze badges




      1,6188 silver badges14 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          6














          [dcl.enum]/5:




          ... If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type ...




          That is, each enumerator has type int until the closing brace is encountered. After that point, the enumerators have type FileCopy and you would not be able to bitwise-OR them together like this anymore.






          share|improve this answer




















          • 2





            And this also allows you to use other arithmetic operations, e.g. enum class A, B = A + 2 ;

            – Ben Voigt
            8 hours ago











          • Thanks! That's exactly the language I needed to see.

            – Diab Jerius
            5 hours ago


















          4














          According to the C++17 Standard (8.5.13 Bitwise inclusive OR operator)




          1 The usual arithmetic conversions (8.3) are performed; the result is
          the bitwise inclusive OR function of its operands. The operator
          applies only to integral or unscoped enumeration operands.




          And (10.2 Enumeration declarations)




          1. ... For a scoped enumeration type, the underlying type is int if it is not
            explicitly specified. In both of these cases, the underlying
            type is said to be fixed.
            Following the closing brace of an
            enum-specifier, each enumerator has the type of its enumeration. If
            the underlying type is fixed, the type of each enumerator prior to the
            closing brace is the underlying type
            and the constant-expression in
            the enumerator-definition shall be a converted constant expression of
            the underlying type



          So this is explicitly documented behavior.






          share|improve this answer



























          • I think within the scoped enum definition, while the type is still incomplete, the elements may have integral type.

            – Ben Voigt
            8 hours ago











          • @BenVoigt You are right.:)

            – Vlad from Moscow
            8 hours ago











          • @Demolishun Thanks. It is a typo.:)

            – Vlad from Moscow
            8 hours ago











          • @VladFromMoscow Thanks!

            – Diab Jerius
            5 hours ago











          • @DiabJerius No at all. You are welcome.:)

            – Vlad from Moscow
            5 hours ago


















          -1














          As long as the value you assign fits in the underlying type of the (possibly scoped) enum, then it's a valid value of the enumeration. It does not have to be one of the explicitly named enum values.



          See http://eel.is/c++draft/dcl.enum#8 for all the technical standards details, but the key point is "... It is possible to define an enumeration that has values not defined by any of its enumerators...".






          share|improve this answer



























          • Thanks for the link! I wasn't aware of it and it helps greatly.

            – Diab Jerius
            5 hours ago











          • I don't see how this answers the question.

            – L. F.
            24 mins ago













          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          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%2f57379757%2fwhy-do-scoped-enums-allow-use-of-operator-when-initializing-using-previously-a%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          [dcl.enum]/5:




          ... If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type ...




          That is, each enumerator has type int until the closing brace is encountered. After that point, the enumerators have type FileCopy and you would not be able to bitwise-OR them together like this anymore.






          share|improve this answer




















          • 2





            And this also allows you to use other arithmetic operations, e.g. enum class A, B = A + 2 ;

            – Ben Voigt
            8 hours ago











          • Thanks! That's exactly the language I needed to see.

            – Diab Jerius
            5 hours ago















          6














          [dcl.enum]/5:




          ... If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type ...




          That is, each enumerator has type int until the closing brace is encountered. After that point, the enumerators have type FileCopy and you would not be able to bitwise-OR them together like this anymore.






          share|improve this answer




















          • 2





            And this also allows you to use other arithmetic operations, e.g. enum class A, B = A + 2 ;

            – Ben Voigt
            8 hours ago











          • Thanks! That's exactly the language I needed to see.

            – Diab Jerius
            5 hours ago













          6












          6








          6







          [dcl.enum]/5:




          ... If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type ...




          That is, each enumerator has type int until the closing brace is encountered. After that point, the enumerators have type FileCopy and you would not be able to bitwise-OR them together like this anymore.






          share|improve this answer













          [dcl.enum]/5:




          ... If the underlying type is fixed, the type of each enumerator prior to the closing brace is the underlying type ...




          That is, each enumerator has type int until the closing brace is encountered. After that point, the enumerators have type FileCopy and you would not be able to bitwise-OR them together like this anymore.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 8 hours ago









          BrianBrian

          70.6k7 gold badges101 silver badges198 bronze badges




          70.6k7 gold badges101 silver badges198 bronze badges










          • 2





            And this also allows you to use other arithmetic operations, e.g. enum class A, B = A + 2 ;

            – Ben Voigt
            8 hours ago











          • Thanks! That's exactly the language I needed to see.

            – Diab Jerius
            5 hours ago












          • 2





            And this also allows you to use other arithmetic operations, e.g. enum class A, B = A + 2 ;

            – Ben Voigt
            8 hours ago











          • Thanks! That's exactly the language I needed to see.

            – Diab Jerius
            5 hours ago







          2




          2





          And this also allows you to use other arithmetic operations, e.g. enum class A, B = A + 2 ;

          – Ben Voigt
          8 hours ago





          And this also allows you to use other arithmetic operations, e.g. enum class A, B = A + 2 ;

          – Ben Voigt
          8 hours ago













          Thanks! That's exactly the language I needed to see.

          – Diab Jerius
          5 hours ago





          Thanks! That's exactly the language I needed to see.

          – Diab Jerius
          5 hours ago













          4














          According to the C++17 Standard (8.5.13 Bitwise inclusive OR operator)




          1 The usual arithmetic conversions (8.3) are performed; the result is
          the bitwise inclusive OR function of its operands. The operator
          applies only to integral or unscoped enumeration operands.




          And (10.2 Enumeration declarations)




          1. ... For a scoped enumeration type, the underlying type is int if it is not
            explicitly specified. In both of these cases, the underlying
            type is said to be fixed.
            Following the closing brace of an
            enum-specifier, each enumerator has the type of its enumeration. If
            the underlying type is fixed, the type of each enumerator prior to the
            closing brace is the underlying type
            and the constant-expression in
            the enumerator-definition shall be a converted constant expression of
            the underlying type



          So this is explicitly documented behavior.






          share|improve this answer



























          • I think within the scoped enum definition, while the type is still incomplete, the elements may have integral type.

            – Ben Voigt
            8 hours ago











          • @BenVoigt You are right.:)

            – Vlad from Moscow
            8 hours ago











          • @Demolishun Thanks. It is a typo.:)

            – Vlad from Moscow
            8 hours ago











          • @VladFromMoscow Thanks!

            – Diab Jerius
            5 hours ago











          • @DiabJerius No at all. You are welcome.:)

            – Vlad from Moscow
            5 hours ago















          4














          According to the C++17 Standard (8.5.13 Bitwise inclusive OR operator)




          1 The usual arithmetic conversions (8.3) are performed; the result is
          the bitwise inclusive OR function of its operands. The operator
          applies only to integral or unscoped enumeration operands.




          And (10.2 Enumeration declarations)




          1. ... For a scoped enumeration type, the underlying type is int if it is not
            explicitly specified. In both of these cases, the underlying
            type is said to be fixed.
            Following the closing brace of an
            enum-specifier, each enumerator has the type of its enumeration. If
            the underlying type is fixed, the type of each enumerator prior to the
            closing brace is the underlying type
            and the constant-expression in
            the enumerator-definition shall be a converted constant expression of
            the underlying type



          So this is explicitly documented behavior.






          share|improve this answer



























          • I think within the scoped enum definition, while the type is still incomplete, the elements may have integral type.

            – Ben Voigt
            8 hours ago











          • @BenVoigt You are right.:)

            – Vlad from Moscow
            8 hours ago











          • @Demolishun Thanks. It is a typo.:)

            – Vlad from Moscow
            8 hours ago











          • @VladFromMoscow Thanks!

            – Diab Jerius
            5 hours ago











          • @DiabJerius No at all. You are welcome.:)

            – Vlad from Moscow
            5 hours ago













          4












          4








          4







          According to the C++17 Standard (8.5.13 Bitwise inclusive OR operator)




          1 The usual arithmetic conversions (8.3) are performed; the result is
          the bitwise inclusive OR function of its operands. The operator
          applies only to integral or unscoped enumeration operands.




          And (10.2 Enumeration declarations)




          1. ... For a scoped enumeration type, the underlying type is int if it is not
            explicitly specified. In both of these cases, the underlying
            type is said to be fixed.
            Following the closing brace of an
            enum-specifier, each enumerator has the type of its enumeration. If
            the underlying type is fixed, the type of each enumerator prior to the
            closing brace is the underlying type
            and the constant-expression in
            the enumerator-definition shall be a converted constant expression of
            the underlying type



          So this is explicitly documented behavior.






          share|improve this answer















          According to the C++17 Standard (8.5.13 Bitwise inclusive OR operator)




          1 The usual arithmetic conversions (8.3) are performed; the result is
          the bitwise inclusive OR function of its operands. The operator
          applies only to integral or unscoped enumeration operands.




          And (10.2 Enumeration declarations)




          1. ... For a scoped enumeration type, the underlying type is int if it is not
            explicitly specified. In both of these cases, the underlying
            type is said to be fixed.
            Following the closing brace of an
            enum-specifier, each enumerator has the type of its enumeration. If
            the underlying type is fixed, the type of each enumerator prior to the
            closing brace is the underlying type
            and the constant-expression in
            the enumerator-definition shall be a converted constant expression of
            the underlying type



          So this is explicitly documented behavior.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered 8 hours ago









          Vlad from MoscowVlad from Moscow

          148k13 gold badges89 silver badges188 bronze badges




          148k13 gold badges89 silver badges188 bronze badges















          • I think within the scoped enum definition, while the type is still incomplete, the elements may have integral type.

            – Ben Voigt
            8 hours ago











          • @BenVoigt You are right.:)

            – Vlad from Moscow
            8 hours ago











          • @Demolishun Thanks. It is a typo.:)

            – Vlad from Moscow
            8 hours ago











          • @VladFromMoscow Thanks!

            – Diab Jerius
            5 hours ago











          • @DiabJerius No at all. You are welcome.:)

            – Vlad from Moscow
            5 hours ago

















          • I think within the scoped enum definition, while the type is still incomplete, the elements may have integral type.

            – Ben Voigt
            8 hours ago











          • @BenVoigt You are right.:)

            – Vlad from Moscow
            8 hours ago











          • @Demolishun Thanks. It is a typo.:)

            – Vlad from Moscow
            8 hours ago











          • @VladFromMoscow Thanks!

            – Diab Jerius
            5 hours ago











          • @DiabJerius No at all. You are welcome.:)

            – Vlad from Moscow
            5 hours ago
















          I think within the scoped enum definition, while the type is still incomplete, the elements may have integral type.

          – Ben Voigt
          8 hours ago





          I think within the scoped enum definition, while the type is still incomplete, the elements may have integral type.

          – Ben Voigt
          8 hours ago













          @BenVoigt You are right.:)

          – Vlad from Moscow
          8 hours ago





          @BenVoigt You are right.:)

          – Vlad from Moscow
          8 hours ago













          @Demolishun Thanks. It is a typo.:)

          – Vlad from Moscow
          8 hours ago





          @Demolishun Thanks. It is a typo.:)

          – Vlad from Moscow
          8 hours ago













          @VladFromMoscow Thanks!

          – Diab Jerius
          5 hours ago





          @VladFromMoscow Thanks!

          – Diab Jerius
          5 hours ago













          @DiabJerius No at all. You are welcome.:)

          – Vlad from Moscow
          5 hours ago





          @DiabJerius No at all. You are welcome.:)

          – Vlad from Moscow
          5 hours ago











          -1














          As long as the value you assign fits in the underlying type of the (possibly scoped) enum, then it's a valid value of the enumeration. It does not have to be one of the explicitly named enum values.



          See http://eel.is/c++draft/dcl.enum#8 for all the technical standards details, but the key point is "... It is possible to define an enumeration that has values not defined by any of its enumerators...".






          share|improve this answer



























          • Thanks for the link! I wasn't aware of it and it helps greatly.

            – Diab Jerius
            5 hours ago











          • I don't see how this answers the question.

            – L. F.
            24 mins ago















          -1














          As long as the value you assign fits in the underlying type of the (possibly scoped) enum, then it's a valid value of the enumeration. It does not have to be one of the explicitly named enum values.



          See http://eel.is/c++draft/dcl.enum#8 for all the technical standards details, but the key point is "... It is possible to define an enumeration that has values not defined by any of its enumerators...".






          share|improve this answer



























          • Thanks for the link! I wasn't aware of it and it helps greatly.

            – Diab Jerius
            5 hours ago











          • I don't see how this answers the question.

            – L. F.
            24 mins ago













          -1












          -1








          -1







          As long as the value you assign fits in the underlying type of the (possibly scoped) enum, then it's a valid value of the enumeration. It does not have to be one of the explicitly named enum values.



          See http://eel.is/c++draft/dcl.enum#8 for all the technical standards details, but the key point is "... It is possible to define an enumeration that has values not defined by any of its enumerators...".






          share|improve this answer















          As long as the value you assign fits in the underlying type of the (possibly scoped) enum, then it's a valid value of the enumeration. It does not have to be one of the explicitly named enum values.



          See http://eel.is/c++draft/dcl.enum#8 for all the technical standards details, but the key point is "... It is possible to define an enumeration that has values not defined by any of its enumerators...".







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago

























          answered 7 hours ago









          Jesper JuhlJesper Juhl

          20.8k3 gold badges29 silver badges52 bronze badges




          20.8k3 gold badges29 silver badges52 bronze badges















          • Thanks for the link! I wasn't aware of it and it helps greatly.

            – Diab Jerius
            5 hours ago











          • I don't see how this answers the question.

            – L. F.
            24 mins ago

















          • Thanks for the link! I wasn't aware of it and it helps greatly.

            – Diab Jerius
            5 hours ago











          • I don't see how this answers the question.

            – L. F.
            24 mins ago
















          Thanks for the link! I wasn't aware of it and it helps greatly.

          – Diab Jerius
          5 hours ago





          Thanks for the link! I wasn't aware of it and it helps greatly.

          – Diab Jerius
          5 hours ago













          I don't see how this answers the question.

          – L. F.
          24 mins ago





          I don't see how this answers the question.

          – L. F.
          24 mins ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57379757%2fwhy-do-scoped-enums-allow-use-of-operator-when-initializing-using-previously-a%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單