Consider the following scenario: template<typename T> struct Base { // Default-constructible Base() = default; // Not copy-constructible Base(Base const &) = delete; }; template<typename T> struct Derived : Base<T> { Derived() = default; Derived(Derived const& d) : Base<T>(d) {} }; // This assertion passes? static_assert( std::is_copy_constructible_v<Derived<int>>); Why does this assertion pass? It is plainly evident that you cannot copy a Derived<int> because doing so will try to copy the Base<int>, which is not copyable. Indeed, if you try to copy it, you get an error: void example(Derived<int>& d) { Derived<int> d2(d); // msvc: error C2280: 'Base<T>::Base(const Base<T> &)': // attempting to reference a deleted function // gcc: error: use of deleted function 'Base<T>::Base(const Base<T>&) // [with T = int]' // clang: error: call to deleted constructor of 'Base<int>' } Okay, so the compiler thinks that Derived<int> is copy-constructible, but then when we try to do it, we find out that it isn’t! What’s going on is that the compiler is determining copy-constructibility by checking whether the class has a non-deleted copy constructor. And in the case of Derived<T> it does haev a non-deleted copy constructor. You declared it yourself! Derived(Derived const& d) : Base<T>(d) {} So yes, there is a copy constructor. It can’t be instantiated, but the compiler doesn’t care. It is going based on what you tell it, and you told it that you can copy it. After all, another possibly copy constructor would have been Derived(Derived const& d) : Base<T>() {} and this one instantiates successfully. Copying a Derived default-constructs the Base base class rather than copy-constructing it. Imagine that we moved the definition out of line. template<typename T> struct Derived : Base<T> { Derived() = default; Derived(Derived const& d); }; What should the answer to the question “Is this copy-constructible?” be? You don’t know what the definition is, only its declaration. Should the compiler...
First seen: 2025-06-10 13:23
Last seen: 2025-06-10 14:23