Vincent
Vincent

Reputation: 60341

CRTP : returning a reference to the derived class?

By what have I to replace the missing line to have this CRTP solution working ?

template<class Crtp> class Base
{
    public:
        inline Crtp& operator=(const Base<Crtp>& rhs)
        {
            for (unsigned int i = 0; i < const_size; ++i) {
                _data[i] = rhs._data[i];
            }
            return /* SOMETHING HERE BUT WHAT ? */
        }

    protected:
        static const unsigned int const_size = 10;
        double _data[const_size];
};

class Derived : public Base<Derived>
{
};

Other question : does the solution you will provide has a cost at running time (compared to the solution where the operator is directly implemented in the derived class) ?

Thank you very much.

Upvotes: 2

Views: 832

Answers (1)

eq-
eq-

Reputation: 10096

return static_cast<Crtp&>(*this);

This has no cost at run time (but you might want to protect the constructor of Base).

Upvotes: 2

Related Questions