使用constexpr进行错误编译

vla*_*378 3 c++ constexpr c++14

我正在尝试编译以下代码:

enum class Order : char
{
    Little,
    Big
};

constexpr Order get_order() {
    uint16_t x = 1;
    return *((uint8_t *) &x) == 0 ? Order::Big : Order::Little;
}
Run Code Online (Sandbox Code Playgroud)

我用-std=c++14标志做到这一点,但我得到了这个错误:

在函数'constexpr byteorder :: Order byteorder :: get_order()'中:/home/user/dev/c++/render/include/byteorder.h:19:1:error:constexpr function'constexpr byteorder :: Order byteorder :: get_order()'不是返回语句

看起来像c ++ 11!

如果c ++ 14在constexpr函数中允许局部变量怎么办?

Debian Jessie,gcc 4.9.2

How*_*ant 5

lang声:

test.cpp:9:17: error: constexpr function never produces a constant expression
    [-Winvalid-constexpr]
constexpr Order get_order() {
                ^
test.cpp:11:14: note: cast that performs the conversions of a reinterpret_cast
is not allowed in a constant expression
    return *((uint8_t *) &x) == 0 ? Order::Big : Order::Little;
             ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)