c++static-membersin-class-initialization

Is the initializer for a const static data member considered a default member initializer?


Is the initializer for a const static data member considered a default member initializer?

The relevant wording is [class.mem.general]/10:

A brace-or-equal-initializer shall appear only in the declaration of a data member. (For static data members, see [class.static.data]; for non-static data members, see [class.base.init] and [dcl.init.aggr]). A brace-or-equal-initializer for a non-static data member specifies a default member initializer for the member [..]

So for example:

constexpr int f() { return 0; }

struct A {
  static const int I = f();
};

Is the brace-or-equal-initializer f() considered a default member initializer?


Solution

  • No.

    Static data members aren't initialised in constructors. f() is just the initialiser for A::I.

    A default member initialiser is used to initialise a non-static data member in each constructor where the mem-initializer-list doesn't otherwise initialise that member. That is, it's a default for the initialisers of that member.

    [class.base.init#9]