0

I have a template base class that looks roughly like this

Vector.cc:

template<typename T, unsigned int D>
class Vector {

    private:
        T _data[D];

        ...

    public:
        Vector(Vector<T, D>&&);

        ...

    public:
        Vector<T, D>& operator=(Vector<T, D>&&);;
};

extern template class Vector<int, 2>;
extern template class Vector<int, 3>;
extern template class Vector<int, 4>;

Vector.h

// include guard

#include "Vector.h"

template<typename T, unsigned int D>
Vector<T, D>::Vector(Vector<T, D>&&) = default;

template<typename T, unsigned int D>
Vector<T, D>& Vector<T, D>::operator=(Vector<T, D>&&) = default;

template class Vector<int, 2>;
template class Vector<int, 3>;
template class Vector<int, 4>;

// include guard

When i compile this i get the errors error: array used as initializer and error: invalid array assignment and a warning note: synthesized method [...] first required here.

When I put the = default; into the declaration in the .h file i get no errors or warnings.

I've read in multiple sources that i can put the = default; into the definition but for me it doesn't work.

What am I doing wrong? Am I missing something? Or did I simply rely on the wrong sources?

3
  • 1
    I have a template base class that looks roughly like this -- Compile errors require you to post a small but complete example of what doesn't compile, not a rough estimate of the code. Commented May 7, 2020 at 18:57
  • 2
    int array[5]; int array2[5]; array = array2; this is not valid in C++. You are more or less getting the same error. Commented May 7, 2020 at 18:58
  • Just out of curiosity: I have never seen someone writing =default outside of the class. What's your reason to do it? It's more code to write and I don't see a benefit. Commented May 7, 2020 at 18:59

1 Answer 1

1

The issue is that the default move is not valid. Inside the class, presumably the compiler doesn't bother generating the code for it, since its likely not used. Putting it outside the class might cause the compiler to decide to compile the operator/constructor.

Think of it in a basic example:

int array[5]; 
int array2[5]; 
array = array2;

The above is not valid in C++, since arrays cannot be assigned to each other.

You would have to create your own move operator/constructor for your class in this case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.