Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

71 views
Skip to first unread message

Joost Kraaijeveld

unread,
Sep 29, 2012, 11:10:56 AM9/29/12
to
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

Victor Bazarov

unread,
Sep 29, 2012, 11:14:59 AM9/29/12
to
On 9/29/2012 11:10 AM, Joost Kraaijeveld wrote:
> 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
--
I do not respond to top-posted replies, please don't ask

SG

unread,
Oct 1, 2012, 6:56:20 AM10/1/12
to
On Saturday, September 29, 2012 5:10:58 PM UTC+2, Joost Kraaijeveld wrote:
> 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
0 new messages