Why is base constructor available via curly braces but not via parentheses?

59 Views Asked by At

I've come across the following issue. Here is a simple piece of code:

struct Base {
  Base(int x) : x_{x} {}

  int get() { return x_; }

private:
  int x_ = 0;
};

struct Derived : Base {};

int main() {
  Derived d{5}; // Works fine
  std::cout << d.get() << std::endl;

  Derived dd(5); // Won't work
  std::cout << dd.get() << std::endl;
}

I have a base class which has a user-defined constructor and a derived one. As far as I know, it is necessary to bring the parent's constructor into the derived scope in order to make it available for the derived, so it's worth mentioning I haven't done that. So I can't really understand, why I can invoke the parent's constructor with curly braces but cannot with parentheses. Does anyone have an answer?

Remarkably, the code won't compile with the latest GCC or Clang, but works just fine with MSVC.

0

There are 0 best solutions below