Search code examples
c++initializationlanguage-lawyerundefined-behavior

Is the evaluation of static member function during construction before base initialization undefined behavior?


I am wondering if this exemple evokes undefined behavior:

struct Base
  {
  Base(int);
  };
struct Derived
  :Base
  {
  static int a_static_function();

  Derived()
    :Base(a_static_function())//is it undefined behavior?
    {}
  };

EDIT: I am asking that because of this paragraph in the C++ standard [class.base.init]:

Member functions (including virtual member functions, 13.3) can be called for an object under construction. Similarly, an object under construction can be the operand of the typeid operator (8.2.8) or of a dynamic_- cast (8.2.7). However, if these operations are performed in a ctor-initializer (or in a function called directly or indirectly from a ctor-initializer) before all the mem-initializers for base classes have completed, the program has undefined behavior.

It does not seems to be specific to non-static member functions.


Solution

  • No, it is not UB.

    A static member function is like an ordinary (non-member) function, except that it has access to private stuff (e.g.private static data, and other static member function).

    My understanding of your citation of the standard is that it applies for non-static member functions.