static member initialization

T

trinisi.news

Hello,

I have a quick question about syntax and class static variables. I have
a non-copyable (boost::noncopyable, in fact) static member variable. I
have to therefore initialize it in some TU.

class noncopyable_class : boost::noncopyable { /* ... */ };

class example {
/* ... */
static non_copyable_class xyxxy;
};

/* ... */

example::xyxxy = non_copyable_class(); /* nope, no copies allowed */
example::xyxxy(non_copyable_class()); /* nope, same thing */
example::xyxxy; /* looks like a declaration, not a definition, to me */

As it stands, I've used this last version. It looks an awful lot like
a declaration to me, however, so I'm wondering if I have UB because of
a non-defined static member variable?
 
V

Victor Bazarov

I have a quick question about syntax and class static variables. I
have a non-copyable (boost::noncopyable, in fact) static member
variable. I have to therefore initialize it in some TU.

All static data members need to be defined in some TU, if they are used
outside of the class definition.
class noncopyable_class : boost::noncopyable { /* ... */ };

class example {
/* ... */
static non_copyable_class xyxxy;

The type is not spelled the same way, BTW.
};

/* ... */

example::xyxxy = non_copyable_class(); /* nope, no copies allowed */

Where is the type. This is assignment, not a declaration.
example::xyxxy(non_copyable_class()); /* nope, same thing */

This looks like a function call.
example::xyxxy; /* looks like a declaration, not a definition, to me
*/

This is an expression that evaluates that xyxxy, and does nothing else.
As it stands, I've used this last version. It looks an awful lot like
a declaration to me, however, so I'm wondering if I have UB because of
a non-defined static member variable?

In all three cases you're missing the type. The _definition_ should be

noncopyable_class example::xyxxy;

and if there is a parameterized initialisation, add the parentheses and
the arguments (and since you didn't show any parameterized constructor
in your 'noncopyable_class', I won't use it);

V
 

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

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top