Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize enum value with function call

Tags:

c++

enums

Is there any way to initialize enum value like this:

enum Test
{
    X = function("X")
};

Currently in Visual Studio I get this error:

error C2057: expected constant expression

And Visual Studio doesn't support constexpr

like image 898
nikitablack Avatar asked May 16 '26 01:05

nikitablack


1 Answers

The enumerator list within an enum declaration is defined as follows:

enumerator-list - comma-separated list of enumerator definitions, each of which is either simply an identifier, which becomes the name of the enumerator, or an identifier with an initializer: identifier = constexpr

You may only use constant expressions.

like image 193
Laura Maftei Avatar answered May 17 '26 14:05

Laura Maftei