1

A simple question but I can not find the set of rules that proves that the behavior of the following code example is correct. It seems here that only strDerived is moved from b, but strBase is copied? Will the implicitly declared move assignment operator of Derived (=> rule of zero) always call the implicitly declared copy assignment operator of Base when it is not finding any move assignment operator there? Is that guaranteed? (Does the same apply when doing move-construction instead of move-assignment in the example?)

#include <string>

struct Base
{
    ~Base() = default;
    std::string strBase{ "Base"};
};

struct Derived : public Base
{
    std::string strDerived{ "Derived" };
};

int main()
{
    Derived a, b;
    a = std::move( b );
    return 0;
}
SoulfreezerXP
  • 459
  • 1
  • 5
  • 19

1 Answers1

1

Of course: this is the fallback for a C++11 class containing a C++03 (thus copy-only) class as a (member or base) subobject. Apparently early versions of the rules would break explicitly defaulting such a move constructor, but that’s long been fixed: in the current draft, [class.copy.ctor]/10.1 refers merely to overload resolution failing rather than to a specific corresponding special member function.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Can you show me plz in this document, where it is fixed/mentioned exactly? it has to be in §15.8.2 right? http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf – SoulfreezerXP Mar 12 '23 at 17:54
  • Maybe its the following note in §15.8.1 (8.4): Note: When the move constructor is not implicitly declared or explicitly supplied, expressions that otherwise would have invoked the move constructor may instead invoke a copy constructor. What do you think about this? Is this the right reference or does it need to be interpreted in a different context here? – SoulfreezerXP Mar 13 '23 at 18:40
  • 1
    @SoulfreezerXP: That note is certainly relevant, although it merely explains the behavior of certain defaulted move constructors rather than giving them that behavior (or rather explaining how it is given). – Davis Herring Mar 14 '23 at 02:02
  • Ok I'll wait for more answers and if no one has anything more to add, then your answer is probably the most correct and I'll mark it accordingly. However, the whole thing is not really satisfactory for me. – SoulfreezerXP Mar 14 '23 at 07:23