Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting this error: constexpr' is not valid here

I have this code:

class myClass
{
        constexpr int x = 4;
};

and on visual studio 2015, I am getting this error:

'constexpr' is not valid here

Why I am getting this error? I want a const static variable that I can initlaize it on header file.

In the next step I want to change my class to a template, but this constant is not related to the type of clas.

like image 217
mans Avatar asked May 14 '18 14:05

mans


2 Answers

Non-static data members cannot be declared as constexpr. Use

class myClass
{
    static constexpr int x = 4;
};

instead.

like image 177
Vittorio Romeo Avatar answered Nov 10 '22 19:11

Vittorio Romeo


I want a const static variable that I can initlaize it on header file

if your main concern is constant value which is shareable to all template type instances, then you can just change to the below:

class myClass
{
    static const int x = 4;
};

If your concern is the memory space (although it is shared between all instances), you can just use compilation pre-processing solution (i.e. #Define X 4)

like image 1
Gal Keren Avatar answered Nov 10 '22 20:11

Gal Keren