Why does the remote copy constructor not allow the use of another constructor with a polymorphic type?

I wonder why this program does not compile (the same behavior in msvc, gcc and clang):

#include <iostream>

using namespace std;

struct Action
{
    virtual void action()
    {
        cout << "Action::action()\n";
    }
};

struct ActionDecorator : Action
{
    ActionDecorator(const ActionDecorator&) = delete;
    ActionDecorator(Action & action) : origAction(action)
    {
    }

    void action() override
    {
        decoration();
        origAction.action();
    }

private:
    void decoration()
    {
        cout << "ActionDecorator::decoration()\n";
    }

    Action & origAction;
};

int main()
{
    Action action;
    ActionDecorator actionDecorator(action);
    ActionDecorator actionDecorator2(actionDecorator);
    actionDecorator2.action();
}

According to my expectation, the remote copy instance should allow another ActionDecorator instance to create an ActionDecorator, since it is a polymorphic type of Action. Instead, I must explicitly specify an ActionDecorator instance for Action & since the compiler complains about trying to reference a remote copy instance. Is there any standard rule to explain this behavior?

+4
source share
1 answer

a . . , , .

c'tor , c'tor, , , .

. c'tor . , c'tor . .

, , , . . - :

ActionDecorator decorate(Action& action) {
  return {action};
}

, . ActionDecorator decorate, Action . c'tor.

, ++ 17.

+6

Source: https://habr.com/ru/post/1694646/


All Articles