Why does the scoped enum support operator '<' by default?












12















Consider:



enum class Number {one, two};

if (Number::one < Number::two)
{}


My understanding is that the scoped enum needs to be cased into the underlying type or integer, and then it can be applied to operator < > ==. But it looks like the code snippet above can work without any explicit overloading operator <.



I don't see any descriptions in Enumeration declaration.



What does the C++ standard say about which operators are supported for a scoped enum by default?










share|improve this question

























  • The standard doesn't have a specific section for this. You need to check each expression and see if it support enumerations. It will say unscoped enumeration it if doesn't support scoped enumerations.

    – NathanOliver
    Jan 28 at 14:09


















12















Consider:



enum class Number {one, two};

if (Number::one < Number::two)
{}


My understanding is that the scoped enum needs to be cased into the underlying type or integer, and then it can be applied to operator < > ==. But it looks like the code snippet above can work without any explicit overloading operator <.



I don't see any descriptions in Enumeration declaration.



What does the C++ standard say about which operators are supported for a scoped enum by default?










share|improve this question

























  • The standard doesn't have a specific section for this. You need to check each expression and see if it support enumerations. It will say unscoped enumeration it if doesn't support scoped enumerations.

    – NathanOliver
    Jan 28 at 14:09
















12












12








12








Consider:



enum class Number {one, two};

if (Number::one < Number::two)
{}


My understanding is that the scoped enum needs to be cased into the underlying type or integer, and then it can be applied to operator < > ==. But it looks like the code snippet above can work without any explicit overloading operator <.



I don't see any descriptions in Enumeration declaration.



What does the C++ standard say about which operators are supported for a scoped enum by default?










share|improve this question
















Consider:



enum class Number {one, two};

if (Number::one < Number::two)
{}


My understanding is that the scoped enum needs to be cased into the underlying type or integer, and then it can be applied to operator < > ==. But it looks like the code snippet above can work without any explicit overloading operator <.



I don't see any descriptions in Enumeration declaration.



What does the C++ standard say about which operators are supported for a scoped enum by default?







c++ enums






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 28 at 19:25









Peter Mortensen

13.8k1987113




13.8k1987113










asked Jan 28 at 14:02









SSYSSY

31219




31219













  • The standard doesn't have a specific section for this. You need to check each expression and see if it support enumerations. It will say unscoped enumeration it if doesn't support scoped enumerations.

    – NathanOliver
    Jan 28 at 14:09





















  • The standard doesn't have a specific section for this. You need to check each expression and see if it support enumerations. It will say unscoped enumeration it if doesn't support scoped enumerations.

    – NathanOliver
    Jan 28 at 14:09



















The standard doesn't have a specific section for this. You need to check each expression and see if it support enumerations. It will say unscoped enumeration it if doesn't support scoped enumerations.

– NathanOliver
Jan 28 at 14:09







The standard doesn't have a specific section for this. You need to check each expression and see if it support enumerations. It will say unscoped enumeration it if doesn't support scoped enumerations.

– NathanOliver
Jan 28 at 14:09














2 Answers
2






active

oldest

votes


















12














If you are referring to the "usual arithmetic conversions", then yes they are done when the arguments are arithmetic or enumeration types. It's just that there is a special bullet there for scoped enums:




[expr]



11 Many binary operators that expect operands of arithmetic or
enumeration type cause conversions and yield result types in a similar
way. The purpose is to yield a common type, which is also the type of
the result. This pattern is called the usual arithmetic conversions,
which are defined as follows:




  • If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.




So this case is covered here. Since the two operands are of the same scoped enum type, they are just checked to hold the specific relation in the fashion one would expect.






share|improve this answer































    10















    My understanding is that scoped enum needs to be cased into underlying type or integer then it can be applied to operator < > ==.




    Not when both of them are scoped enums. SomeScopedEnum < SomeInt is ill-formed, you're right in that case.



    [expr.rel]p6:




    If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.







    share|improve this answer
























    • Can I say operator < will use underlying integer to compare the scoped enum elements if they are same type?

      – SSY
      Jan 28 at 14:10






    • 1





      @SSY Yes, you can.

      – Max Langhof
      Jan 28 at 14:14












    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%2f54403632%2fwhy-does-the-scoped-enum-support-operator-by-default%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    If you are referring to the "usual arithmetic conversions", then yes they are done when the arguments are arithmetic or enumeration types. It's just that there is a special bullet there for scoped enums:




    [expr]



    11 Many binary operators that expect operands of arithmetic or
    enumeration type cause conversions and yield result types in a similar
    way. The purpose is to yield a common type, which is also the type of
    the result. This pattern is called the usual arithmetic conversions,
    which are defined as follows:




    • If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.




    So this case is covered here. Since the two operands are of the same scoped enum type, they are just checked to hold the specific relation in the fashion one would expect.






    share|improve this answer




























      12














      If you are referring to the "usual arithmetic conversions", then yes they are done when the arguments are arithmetic or enumeration types. It's just that there is a special bullet there for scoped enums:




      [expr]



      11 Many binary operators that expect operands of arithmetic or
      enumeration type cause conversions and yield result types in a similar
      way. The purpose is to yield a common type, which is also the type of
      the result. This pattern is called the usual arithmetic conversions,
      which are defined as follows:




      • If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.




      So this case is covered here. Since the two operands are of the same scoped enum type, they are just checked to hold the specific relation in the fashion one would expect.






      share|improve this answer


























        12












        12








        12







        If you are referring to the "usual arithmetic conversions", then yes they are done when the arguments are arithmetic or enumeration types. It's just that there is a special bullet there for scoped enums:




        [expr]



        11 Many binary operators that expect operands of arithmetic or
        enumeration type cause conversions and yield result types in a similar
        way. The purpose is to yield a common type, which is also the type of
        the result. This pattern is called the usual arithmetic conversions,
        which are defined as follows:




        • If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.




        So this case is covered here. Since the two operands are of the same scoped enum type, they are just checked to hold the specific relation in the fashion one would expect.






        share|improve this answer













        If you are referring to the "usual arithmetic conversions", then yes they are done when the arguments are arithmetic or enumeration types. It's just that there is a special bullet there for scoped enums:




        [expr]



        11 Many binary operators that expect operands of arithmetic or
        enumeration type cause conversions and yield result types in a similar
        way. The purpose is to yield a common type, which is also the type of
        the result. This pattern is called the usual arithmetic conversions,
        which are defined as follows:




        • If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.




        So this case is covered here. Since the two operands are of the same scoped enum type, they are just checked to hold the specific relation in the fashion one would expect.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 28 at 14:13









        StoryTellerStoryTeller

        104k12218281




        104k12218281

























            10















            My understanding is that scoped enum needs to be cased into underlying type or integer then it can be applied to operator < > ==.




            Not when both of them are scoped enums. SomeScopedEnum < SomeInt is ill-formed, you're right in that case.



            [expr.rel]p6:




            If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.







            share|improve this answer
























            • Can I say operator < will use underlying integer to compare the scoped enum elements if they are same type?

              – SSY
              Jan 28 at 14:10






            • 1





              @SSY Yes, you can.

              – Max Langhof
              Jan 28 at 14:14
















            10















            My understanding is that scoped enum needs to be cased into underlying type or integer then it can be applied to operator < > ==.




            Not when both of them are scoped enums. SomeScopedEnum < SomeInt is ill-formed, you're right in that case.



            [expr.rel]p6:




            If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.







            share|improve this answer
























            • Can I say operator < will use underlying integer to compare the scoped enum elements if they are same type?

              – SSY
              Jan 28 at 14:10






            • 1





              @SSY Yes, you can.

              – Max Langhof
              Jan 28 at 14:14














            10












            10








            10








            My understanding is that scoped enum needs to be cased into underlying type or integer then it can be applied to operator < > ==.




            Not when both of them are scoped enums. SomeScopedEnum < SomeInt is ill-formed, you're right in that case.



            [expr.rel]p6:




            If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.







            share|improve this answer














            My understanding is that scoped enum needs to be cased into underlying type or integer then it can be applied to operator < > ==.




            Not when both of them are scoped enums. SomeScopedEnum < SomeInt is ill-formed, you're right in that case.



            [expr.rel]p6:




            If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 28 at 14:06









            Rakete1111Rakete1111

            35.2k1084119




            35.2k1084119













            • Can I say operator < will use underlying integer to compare the scoped enum elements if they are same type?

              – SSY
              Jan 28 at 14:10






            • 1





              @SSY Yes, you can.

              – Max Langhof
              Jan 28 at 14:14



















            • Can I say operator < will use underlying integer to compare the scoped enum elements if they are same type?

              – SSY
              Jan 28 at 14:10






            • 1





              @SSY Yes, you can.

              – Max Langhof
              Jan 28 at 14:14

















            Can I say operator < will use underlying integer to compare the scoped enum elements if they are same type?

            – SSY
            Jan 28 at 14:10





            Can I say operator < will use underlying integer to compare the scoped enum elements if they are same type?

            – SSY
            Jan 28 at 14:10




            1




            1





            @SSY Yes, you can.

            – Max Langhof
            Jan 28 at 14:14





            @SSY Yes, you can.

            – Max Langhof
            Jan 28 at 14:14


















            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%2f54403632%2fwhy-does-the-scoped-enum-support-operator-by-default%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

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules