constexpr允许函数体中的reinterpret_cast [英] constexpr allows reinterpret_cast in function body
问题描述
这是非标准的,并且clang会正确地抱怨它。我们的编译器也应该抱怨这个。
This is non-standard, and clang will correctly complain about it. Our compiler should also complain about this.
推荐答案
你确定你对Clang是对的。
Are you sure you are correct about Clang.
记住, Visual C ++默认在C ++ 14模式下编译,无法将其置于C ++ 11模式。因此,在Clang中失败的代码,请尝试使用--std = c ++ 14或--std = c ++ 17.
Remember, Visual C++ compiles in C++14 mode by default and has no way of putting it into C++11 mode. So that code that does fail in Clang, try it with --std=c++14 or --std=c++17.
这很重要因为constexpr的功能是与C ++ 11相比,C ++ 14标准大幅提升。对于C ++ 11,它定义为:
This is important because the capabilities of constexpr was vastly increased with the C++14 standard compared to C++11. For C++11 it was defined as:
- 函数体必须被删除或默认,或者只包含以下内容:
- null statements (plain semicolons)
- static_assert declarations
- typedef declarations and alias declarations that do not define classes or enumerations
- using declarations
- using directives
- exactly one return statement.
虽然C ++ 14是:
While C++14 was:
- 函数体必须删除或默认或包含
除之外的任何语句:
- the function body must be either deleted or defaulted or contain any statements except:
- an asm declaration
- a goto statement
- a statement with a
label other than
case
anddefault
- a try-block
- a definition of a variable of non-literal type
- a definition of a variable of static or thread storage duration
- a definition of a variable for which no initialization is performed.
C ++ 14定义是排他性的,但并未说明不允许使用reinterpret_cast。
The C++14 definition is exclusionary, but it doesn't say that reinterpret_cast isn't allowed.
这篇关于constexpr允许函数体中的reinterpret_cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!