Constexpr if with a non-bool condition

荒凉一梦 提交于 2019-12-04 23:37:13
StoryTeller - Unslander Monica

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!