How to initialise const static char array in a header only class?

J

Joost Kraaijeveld

Hi,

I want to initialise a const static char array in a class that should be
header only. The header will be included by many clients within the same
program. Ordinary int and char variables I can initialise in the class
declaration. But an array fails, e.g.

class Message
{
public:
static const char majorVersion = 1; // OK
static const char minorVersion = 1; // OK
static const char magicNumber[] = {'1','0','0','1'}; // Not OK
}

results in the following compiler (GCC) error:

error: in-class initialization of static data member ‘const char
Message::magicNumber []’ of incomplete type.

Is this a matter of mere syntax or is this something that cannot be
done? Or is there another way to get the same effect, without resorting
to a cpp file?

TIA

Joost
 
V

Victor Bazarov

I want to initialise a const static char array in a class that should be
header only. The header will be included by many clients within the same
program. Ordinary int and char variables I can initialise in the class
declaration. But an array fails, e.g.

class Message
{
public:
static const char majorVersion = 1; // OK
static const char minorVersion = 1; // OK
static const char magicNumber[] = {'1','0','0','1'}; // Not OK
}
;


results in the following compiler (GCC) error:

error: in-class initialization of static data member ‘const char
Message::magicNumber []’ of incomplete type.

Is this a matter of mere syntax or is this something that cannot be
done?

The Standard requires the static constant member to be of integral or
enumeration type if you want to initialize it in the class definition
using the brace-or-equal-initializer ([class.static.data]/3).
Or is there another way to get the same effect, without resorting
to a cpp file?

Nope.

V
 
S

SG

Hi, I want to initialise a const static char array in a class that
should be header only. The header will be included by many clients
within the same program. [...]
Is this a matter of mere syntax or is this something that cannot
be done?

It's not a matter of syntax. Static data members are NEVER defined in a class definition, only declared. As a special exception, the compiler lets you "initialize" _constant_ _integral_ (or scalars in general?) members.
Or is there another way to get the same effect, without resorting to
a cpp file?

Do you have a good reason for defining an array inside a header? Not requiring a CPP file is not a good reason, IMHO.

But you can do things like

typedef int five_ints[5];

inline const five_ints& foo()
{
static const five_ints arr = {1,2,3,5,8};
return arr;
}

in a header file -- or even write

template<class Dummy=void>
class MessageBase
{
public:
static const int arr[5];
};

template<class Dummy>
const int MessageBase<Dummy>::arr[5] = {1,2,3,5,8};

class Message : public MessageBase<>
{
};

since multiple definitions of inline functions and members of class templates are allowed within a program. But the purpose of these rules is to support templates and to ease inlining. It's not to allow users to define arrays in header files.

Cheers!
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top