1

I have this code:

class DoorKeeper;

struct Door {
  class Key {
    Key() = default;
    // ~Key() = default; // [1]
    friend DoorKeeper;
  };
  void open(Key) {}
};

int main(int, char **) {
  Door().open(Door::Key()); // always complain that constructor is private
  Door().open(Door::Key{}); // complains only about destructor [1] if uncommented
}

I would expect the 2 lines in main to behave in the same way, but ends up I do not get any error when using curly braces. I can get an error by adding a member to Key class or by adding a destructor.

What can be the reason for this?

https://godbolt.org/z/zrzYoTKMn

OznOg
  • 4,440
  • 2
  • 26
  • 35

0 Answers0