Why does the scoped enum support operator '<' by default?
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
add a comment |
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
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
add a comment |
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
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
c++ enums
edited Jan 28 at 19:25
Peter Mortensen
13.6k1984111
13.6k1984111
asked Jan 28 at 14:02
SSYSSY
1929
1929
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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 andfalse
if it is false.
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
add a comment |
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
});
}
});
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
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 28 at 14:13
StoryTellerStoryTeller
97.7k12199265
97.7k12199265
add a comment |
add a comment |
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 andfalse
if it is false.
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
add a comment |
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 andfalse
if it is false.
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
add a comment |
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 andfalse
if it is false.
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 andfalse
if it is false.
answered Jan 28 at 14:06
Rakete1111Rakete1111
34.6k1082118
34.6k1082118
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
add a comment |
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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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