Hasty Briefsbeta

Bilingual

Why does C++ think my class is copy-constructible when it can't be?

a year ago
  • #Template
  • #C++
  • #Copy Constructor
  • The assertion `std::is_copy_constructible_v<Derived<int>>` passes because the compiler checks for the presence of a non-deleted copy constructor, not its instantiation.
  • `Derived<int>` has a user-declared copy constructor that is not marked as deleted, so the compiler assumes it is copy-constructible.
  • Attempting to copy `Derived<int>` fails because it tries to copy `Base<int>`, which has a deleted copy constructor.
  • If the copy constructor of `Derived` were defaulted (`= default`), it would be implicitly deleted due to `Base` being non-copyable, causing the assertion to fail.
  • The compiler does not check the instantiation of the copy constructor for `is_copy_constructible`, only its declaration.