Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize static members without repeating their type

Tags:

c++

c++11

Let's say we have the following class:

class A {
    static SomeLongType b;
};

Now we have to initialize it in the appropriate cpp file. I can think of the following ways:

SomeLongType A::b{}; // repetition of SomeLongType
decltype(A::b) A::b{}; // A::b written two times

Both seem to be kind of cumbersome to me. Is there a better way?

like image 290
Appleshell Avatar asked Oct 20 '22 16:10

Appleshell


1 Answers

The perfect solution would be to use C++11 auto. But as ecatmur commented, thats not allowed by the language.

Why not just define a simple macro?

#define DEFINE(x) decltype(x) x{}

struct A
{
    static SomeLongType b;
};

DEFINE( A::b );

I really hate C macros, but they are usefull in certain cases.

like image 158
Manu343726 Avatar answered Oct 23 '22 11:10

Manu343726