Std :: forward with pass-by-value argument

I read about encapsulated polymorphism, and I came across this code:

template <typename T>
struct Model<T> : Concept
{
    Model<T>(T impl) :
        mImpl(std::forward<T>(impl))
    {

    }
    virtual Concept* clone() const override
    {
        return new Model<T>(mImpl)
    }

    virtual void operator (const LogMessage::Meta& meta, const std::string& message) override
    {
        mImpl(meta, message);
    }

    T mImpl;
};

What is the point of forwarding impl to the Model constructor?

Does it make sense to forward the argument if it is passed in a value?

+4
source share
1 answer

If Model<T>, where Tis the reference type lvalue (for example, X&) is legal (according to the model documentation), then forwardis the right tool to use here. Otherwise (if Tit should always be an object type), moveis the right tool.

- clone , T . move . forward , , , OP.

+5

All Articles