c++initializationlanguage-lawyerc++20member-initialization

Is a member initialization list without a value defined?


Say I have :

class Foo
{
public:
  int x;
  Foo() : x() {}
} 

Would it be UB to read x after the constructor has ran? More specifically, what type of initialization is this, zero, direct or default initialization?

I know if instead we'd have:

Foo() : x(42) {}

x would be direct-initialized to 42 but I'm not so sure for the snippet above and I don't want to get bit by the UB wolf if this turns out to be default-initialized.


Solution

  • what type of initialization is this

    x() performs value-initialization:

    when a non-static data member or a base class is initialized using a member initializer with an empty pair of parentheses or braces (since C++11);

    As non-class type int, x is zero-initialized as 0 at last.

    1. otherwise, the object is zero-initialized.