0

I'm writing a vector template with a few specializations so that there are named accessors for the first few items of the vector (such x, y, z, w) depending on the size. KennyTM's answer was a great help with this. I also added a form of Curiously recurring template pattern to be able to use the operators naturally. Here is the base class and a specialization with added functions (omitted most member implementations for brevity):

template<int Dim, typename V, typename ItemType = float>
class VectorImpl
{
public:
    typedef ItemType value_type;    
    VectorImpl() { elements.fill(0); }    
    VectorImpl(std::initializer_list<ItemType> init_list);

        V operator+(const V& other)
    {
        V result;
        for (int i = 0; i < Dim; ++i) 
            result.elements[i] = elements[i] + other.elements[i];

        return result;
    }

    QString toString();    
        // ... other members ...

protected:
    VectorImpl(const VectorImpl& other) = default;

protected:
    std::array<ItemType, Dim> elements;
};

template<int Dim, typename ItemType = float>
class Vector : public VectorImpl<Dim, Vector<Dim, ItemType>, ItemType>
{
    typedef VectorImpl<Dim, Vector<Dim, ItemType>, ItemType> ParentType;
public:
    Vector() : ParentType() {}
    Vector(const ParentType& other) : ParentType(other) {}
    Vector(std::initializer_list<ItemType> init_list) : ParentType(init_list) {}
};

template<typename ItemType>
class Vector<3, ItemType> : public VectorImpl<3, Vector<3, ItemType>, ItemType>
{
    typedef VectorImpl<3, Vector<3, ItemType>, ItemType> ParentType;
public:
    Vector() : ParentType() {}
    Vector(const ParentType& other) : ParentType(other) {}
    Vector(std::initializer_list<ItemType> init_list) : ParentType(init_list) {}
    ItemType x() const { return this->elements[0]; }
    ItemType y() const { return this->elements[1]; }
    ItemType z() const { return this->elements[2]; }
};

And I wanted to add qDebug()<< support, so I did this:

template<int Dim, typename ItemType>
QDebug operator<<(QDebug dbg, Vector<Dim, ItemType>& v)
{
    dbg.nospace() << v.toString();
    return dbg.space();
}

Now, the following code compiles and works:

Vector<3> v1 = { 3,4,5 };
qDebug() << v1;

This one does, too:

Vector<3> v1 = { 3,4,5 };
Vector<3> v2 = { 1,-1,1 };
qDebug() << v1;
auto v3 = v1 + v2;
qDebug() << v3;

But this one does not:

Vector<3> v1 = { 3,4,5 };
Vector<3> v2 = { 1,-1,1 };
qDebug() << (v1 + v2);

The compiler says:

error: no match for 'operator<<' in 'qDebug()() << v1.Vector<3>::.VectorImpl::operator+ [with int Dim = 3, V = Vector<3>, ItemType = float]((*(const Vector<3>*)(& v2)))'

What is going on? Why is the type of v1 + v2 different when assigning to a variable? What should I do to make this compile?

Community
  • 1
  • 1
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180

1 Answers1

4

Give the output function a const reference, or rvalues (such as the temporary that is the result of your addition) won't bind to it (and nor will actual constants, for that matter):

QDebug operator<<(QDebug dbg, Vector<Dim, ItemType> const & v)
//                                                  ^^^^^

Also declare toString as const:

QString toString() const;
//                 ^^^^^
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084