Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialising constexpr - " illegal initialization of 'constexpr' entity with a non-constant expression"

I have two enum class types: Type and SocketType. The following code won't compile and fails with the message mentioned in the question, in VC++ 2017:

static constexpr std::map<Type,SocketType> PacketTypeMap =
    {
        {Type::JUSTJOINED,      SocketType::TCP},
        {Type::CHAT_MESSAGE,    SocketType::TCP},
        {Type::REQUEST_WORLD,   SocketType::TCP},
        {Type::DATA_WORLD,      SocketType::TCP},
        {Type::DATA_PLAYER,     SocketType::UDP},
        {Type::RESPAWN_PLAYER,  SocketType::TCP}
    };

Been trying some variations and nothing works, but I'm sure I'm just missing something simple with the syntax.

like image 353
Bluejay Avatar asked Oct 28 '25 03:10

Bluejay


1 Answers

std::map is not compatible with constexpr. There exists an experimental(?) library called frozen, which provides a constexpr-compatible frozen::map (besides frozen::unordered_map, frozen::string, and others).

However, most probably you just want to pick a simpler solution (e.g., a switch statement in a constexpr function).

like image 117
Julius Avatar answered Oct 29 '25 19:10

Julius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!