Constexpr if with a non-bool condition
Clash Royale CLAN TAG#URR8PPP
I seem to have found something that Clang and GCC disagree on. Here's the code:
int main()
if constexpr (2)
This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message:
test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2)
^
1 error generated.
cppreference doesn't seem to mention "narrowing", so this seems like a Clang bug, but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?
c++ language-lawyer c++17 implicit-conversion compiler-bug
add a comment |
I seem to have found something that Clang and GCC disagree on. Here's the code:
int main()
if constexpr (2)
This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message:
test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2)
^
1 error generated.
cppreference doesn't seem to mention "narrowing", so this seems like a Clang bug, but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?
c++ language-lawyer c++17 implicit-conversion compiler-bug
What if you doif constexpr (!!2)
?
– Jesper Juhl
Feb 27 at 6:48
(!!2) will work, but 2 should work too.
– ivan.ukr
Feb 27 at 6:49
1
I just checked.!!2
works with clang
– Kerndog73
Feb 27 at 6:49
4
bugs.llvm.org/show_bug.cgi?id=39322 gcc.gnu.org/bugzilla/show_bug.cgi?id=87724#c1
– Language Lawyer
Feb 27 at 14:08
add a comment |
I seem to have found something that Clang and GCC disagree on. Here's the code:
int main()
if constexpr (2)
This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message:
test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2)
^
1 error generated.
cppreference doesn't seem to mention "narrowing", so this seems like a Clang bug, but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?
c++ language-lawyer c++17 implicit-conversion compiler-bug
I seem to have found something that Clang and GCC disagree on. Here's the code:
int main()
if constexpr (2)
This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message:
test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2)
^
1 error generated.
cppreference doesn't seem to mention "narrowing", so this seems like a Clang bug, but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?
c++ language-lawyer c++17 implicit-conversion compiler-bug
c++ language-lawyer c++17 implicit-conversion compiler-bug
edited Feb 27 at 13:06
Peter Mortensen
13.8k1987113
13.8k1987113
asked Feb 27 at 6:39
Kerndog73Kerndog73
8761027
8761027
What if you doif constexpr (!!2)
?
– Jesper Juhl
Feb 27 at 6:48
(!!2) will work, but 2 should work too.
– ivan.ukr
Feb 27 at 6:49
1
I just checked.!!2
works with clang
– Kerndog73
Feb 27 at 6:49
4
bugs.llvm.org/show_bug.cgi?id=39322 gcc.gnu.org/bugzilla/show_bug.cgi?id=87724#c1
– Language Lawyer
Feb 27 at 14:08
add a comment |
What if you doif constexpr (!!2)
?
– Jesper Juhl
Feb 27 at 6:48
(!!2) will work, but 2 should work too.
– ivan.ukr
Feb 27 at 6:49
1
I just checked.!!2
works with clang
– Kerndog73
Feb 27 at 6:49
4
bugs.llvm.org/show_bug.cgi?id=39322 gcc.gnu.org/bugzilla/show_bug.cgi?id=87724#c1
– Language Lawyer
Feb 27 at 14:08
What if you do
if constexpr (!!2)
?– Jesper Juhl
Feb 27 at 6:48
What if you do
if constexpr (!!2)
?– Jesper Juhl
Feb 27 at 6:48
(!!2) will work, but 2 should work too.
– ivan.ukr
Feb 27 at 6:49
(!!2) will work, but 2 should work too.
– ivan.ukr
Feb 27 at 6:49
1
1
I just checked.
!!2
works with clang– Kerndog73
Feb 27 at 6:49
I just checked.
!!2
works with clang– Kerndog73
Feb 27 at 6:49
4
4
bugs.llvm.org/show_bug.cgi?id=39322 gcc.gnu.org/bugzilla/show_bug.cgi?id=87724#c1
– Language Lawyer
Feb 27 at 14:08
bugs.llvm.org/show_bug.cgi?id=39322 gcc.gnu.org/bugzilla/show_bug.cgi?id=87724#c1
– Language Lawyer
Feb 27 at 14:08
add a comment |
2 Answers
2
active
oldest
votes
Clang is diagnosing under these paragraphs
[stmt.if] (emphasis mine)
2 If the if statement is of the form if constexpr, the value of
the condition shall be a contextually converted constant expression of
type bool; this form is called a constexpr if statement.
[expr.const]
4 A converted constant expression of type T is an expression,
implicitly converted to type T, where the converted expression is a
constant expression and the implicit conversion sequence contains only
- integral conversions other than narrowing conversions,
Now, when it comes to integral conversions, a conversion to bool
is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int
. So the diagnostic is not without grounds.
But I think it's also quite reasonable to consider the fact a conversion to bool
is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard1, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.
1 - And a proposal exists to change it.
11
A bug in the standard! LOL
– Kerndog73
Feb 27 at 6:57
8
There is a proposal for this, P1401
– Rakete1111
Feb 27 at 7:49
3
@Rakete1111 - shamelessly added to the answer :) Thank you!
– StoryTeller
Feb 27 at 7:53
add a comment |
We say it, but it's hidden. "contextually converted constant expression of type bool
" is a standard term-of-art that excludes narrowing conversions.
Clang is correct.
Did CWG agree that the current wording in the standard is intended?
– Language Lawyer
Feb 27 at 12:46
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%2f54899466%2fconstexpr-if-with-a-non-bool-condition%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
Clang is diagnosing under these paragraphs
[stmt.if] (emphasis mine)
2 If the if statement is of the form if constexpr, the value of
the condition shall be a contextually converted constant expression of
type bool; this form is called a constexpr if statement.
[expr.const]
4 A converted constant expression of type T is an expression,
implicitly converted to type T, where the converted expression is a
constant expression and the implicit conversion sequence contains only
- integral conversions other than narrowing conversions,
Now, when it comes to integral conversions, a conversion to bool
is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int
. So the diagnostic is not without grounds.
But I think it's also quite reasonable to consider the fact a conversion to bool
is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard1, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.
1 - And a proposal exists to change it.
11
A bug in the standard! LOL
– Kerndog73
Feb 27 at 6:57
8
There is a proposal for this, P1401
– Rakete1111
Feb 27 at 7:49
3
@Rakete1111 - shamelessly added to the answer :) Thank you!
– StoryTeller
Feb 27 at 7:53
add a comment |
Clang is diagnosing under these paragraphs
[stmt.if] (emphasis mine)
2 If the if statement is of the form if constexpr, the value of
the condition shall be a contextually converted constant expression of
type bool; this form is called a constexpr if statement.
[expr.const]
4 A converted constant expression of type T is an expression,
implicitly converted to type T, where the converted expression is a
constant expression and the implicit conversion sequence contains only
- integral conversions other than narrowing conversions,
Now, when it comes to integral conversions, a conversion to bool
is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int
. So the diagnostic is not without grounds.
But I think it's also quite reasonable to consider the fact a conversion to bool
is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard1, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.
1 - And a proposal exists to change it.
11
A bug in the standard! LOL
– Kerndog73
Feb 27 at 6:57
8
There is a proposal for this, P1401
– Rakete1111
Feb 27 at 7:49
3
@Rakete1111 - shamelessly added to the answer :) Thank you!
– StoryTeller
Feb 27 at 7:53
add a comment |
Clang is diagnosing under these paragraphs
[stmt.if] (emphasis mine)
2 If the if statement is of the form if constexpr, the value of
the condition shall be a contextually converted constant expression of
type bool; this form is called a constexpr if statement.
[expr.const]
4 A converted constant expression of type T is an expression,
implicitly converted to type T, where the converted expression is a
constant expression and the implicit conversion sequence contains only
- integral conversions other than narrowing conversions,
Now, when it comes to integral conversions, a conversion to bool
is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int
. So the diagnostic is not without grounds.
But I think it's also quite reasonable to consider the fact a conversion to bool
is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard1, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.
1 - And a proposal exists to change it.
Clang is diagnosing under these paragraphs
[stmt.if] (emphasis mine)
2 If the if statement is of the form if constexpr, the value of
the condition shall be a contextually converted constant expression of
type bool; this form is called a constexpr if statement.
[expr.const]
4 A converted constant expression of type T is an expression,
implicitly converted to type T, where the converted expression is a
constant expression and the implicit conversion sequence contains only
- integral conversions other than narrowing conversions,
Now, when it comes to integral conversions, a conversion to bool
is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int
. So the diagnostic is not without grounds.
But I think it's also quite reasonable to consider the fact a conversion to bool
is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard1, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.
1 - And a proposal exists to change it.
edited Feb 28 at 10:15
Rakete1111
35.1k1084119
35.1k1084119
answered Feb 27 at 6:55
StoryTellerStoryTeller
103k12217280
103k12217280
11
A bug in the standard! LOL
– Kerndog73
Feb 27 at 6:57
8
There is a proposal for this, P1401
– Rakete1111
Feb 27 at 7:49
3
@Rakete1111 - shamelessly added to the answer :) Thank you!
– StoryTeller
Feb 27 at 7:53
add a comment |
11
A bug in the standard! LOL
– Kerndog73
Feb 27 at 6:57
8
There is a proposal for this, P1401
– Rakete1111
Feb 27 at 7:49
3
@Rakete1111 - shamelessly added to the answer :) Thank you!
– StoryTeller
Feb 27 at 7:53
11
11
A bug in the standard! LOL
– Kerndog73
Feb 27 at 6:57
A bug in the standard! LOL
– Kerndog73
Feb 27 at 6:57
8
8
There is a proposal for this, P1401
– Rakete1111
Feb 27 at 7:49
There is a proposal for this, P1401
– Rakete1111
Feb 27 at 7:49
3
3
@Rakete1111 - shamelessly added to the answer :) Thank you!
– StoryTeller
Feb 27 at 7:53
@Rakete1111 - shamelessly added to the answer :) Thank you!
– StoryTeller
Feb 27 at 7:53
add a comment |
We say it, but it's hidden. "contextually converted constant expression of type bool
" is a standard term-of-art that excludes narrowing conversions.
Clang is correct.
Did CWG agree that the current wording in the standard is intended?
– Language Lawyer
Feb 27 at 12:46
add a comment |
We say it, but it's hidden. "contextually converted constant expression of type bool
" is a standard term-of-art that excludes narrowing conversions.
Clang is correct.
Did CWG agree that the current wording in the standard is intended?
– Language Lawyer
Feb 27 at 12:46
add a comment |
We say it, but it's hidden. "contextually converted constant expression of type bool
" is a standard term-of-art that excludes narrowing conversions.
Clang is correct.
We say it, but it's hidden. "contextually converted constant expression of type bool
" is a standard term-of-art that excludes narrowing conversions.
Clang is correct.
answered Feb 27 at 6:55
T.C.T.C.
108k14221331
108k14221331
Did CWG agree that the current wording in the standard is intended?
– Language Lawyer
Feb 27 at 12:46
add a comment |
Did CWG agree that the current wording in the standard is intended?
– Language Lawyer
Feb 27 at 12:46
Did CWG agree that the current wording in the standard is intended?
– Language Lawyer
Feb 27 at 12:46
Did CWG agree that the current wording in the standard is intended?
– Language Lawyer
Feb 27 at 12:46
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%2f54899466%2fconstexpr-if-with-a-non-bool-condition%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
What if you do
if constexpr (!!2)
?– Jesper Juhl
Feb 27 at 6:48
(!!2) will work, but 2 should work too.
– ivan.ukr
Feb 27 at 6:49
1
I just checked.
!!2
works with clang– Kerndog73
Feb 27 at 6:49
4
bugs.llvm.org/show_bug.cgi?id=39322 gcc.gnu.org/bugzilla/show_bug.cgi?id=87724#c1
– Language Lawyer
Feb 27 at 14:08