MoonKnight
MoonKnight

Reputation: 23833

Copy ctor not Operating as Expected in VS2015

I have a small class for performing basic/lightweight matrix operations.

template<class E, int Rows, int Cols>
class TinyExpression {
public:
    static const int ROWS = Rows;
    static const int COLS = Cols;

    inline const E& operator () () const 
    {
        return *static_cast<const E*> (this);
    }
    inline E& operator () () 
    {
        return *static_cast<E*> (this);
    }
protected:
    TinyExpression() {}
    ~TinyExpression() {}
private:
    const TinyExpression& operator=(const TinyExpression &);
    TinyExpression(const TinyExpression&);
};

template <class M, int Rows, int Cols>
class TinyNegate : public TinyExpression<TinyNegate<M, Rows, Cols>, Rows, Cols>
{
public:
    typedef typename M::value_type value_type;
    TinyNegate(const M& m) : _m(m) {};
    inline int size() const { return _m.size(); }
    inline int size1() const { return _m.size1(); }
    inline int size2() const { return _m.size2(); }
    inline value_type operator()(int r, int c) const { return -_m(r, c); }
private:
    const M& _m;
};

In my TinyMatrix class I have

template <int Rows, int Cols>
class TinyMatrix : public TinyExpression<TinyMatrix<Rows, Cols>, Rows, Cols>
{
public:
    typedef double value_type;

    TinyMatrix()
    {
        std::fill(_data, _data + Rows * Cols, 0.0);
    }

    TinyMatrix(const TinyMatrix<Rows, Cols>& m) 
    {
        std::copy(m.begin(), m.begin() + size(), &_data[0]);
    }

    ... // Lots of other stuff.

    TinyNegate<TinyMatrix<Rows, Cols>, Rows, Cols> operator-() const
    {
        return TinyNegate<TinyMatrix<Rows, Cols>, Rows, Cols>(*this); // THIS IS THE OFFENDING LINE
    }
}

The offending code is marked and is return TinyNegate<TinyMatrix<Rows, Cols>, Rows, Cols>(*this);. This code was working fine in VS2013, now in VS2015 it does not compile with the following error

Severity Code Description Project File Line Suppression State Error C2280 'TinyNegate,2,1>::TinyNegate(const TinyNegate,2,1> &)': attempting to reference a deleted function MyProject f:\src\math\matrix\tiny_matrix.h 126

But I am correctly defining an explicit copy constructor using a reference value. I could not use the copy-ctor and use direct initialization but this would give me obvious errors regarding my const types. This worked in VS2013 and on gcc...

What evil is the new C++ compiler that ships with VS2015 doing, why am I getting this error and how can I fix this?

Thanks for your time.

Upvotes: 2

Views: 217

Answers (1)

MarkusParker
MarkusParker

Reputation: 1596

The error message does not refer to the constructor

TinyNegate(const M& m) : _m(m) {};

which is in fact not a copy constructor. The actual copy constructor looks like

TinyNegate(const TinyNegate& t) : _m(t._m) {};

Add it and the code should compile. Note that you do not need to specify the template parameters for the argument type TinyNegate.

Upvotes: 1

Related Questions