Вопрос

I have the following code which causes problem when I call operator=() on a Property instance:

// myProperty.h.

template <class T, int typeFamily = TypeFamily<T>::value>
class PropertyImpl : public PropertyBase
{
    // Default template to catch errors.
};

template <class T>
class PropertyImpl<T, 0> : public PropertyBase
{
    // Data is part of the family of base types.
public:
    PropertyImpl(T* dataRef) : PropertyBase(), m_dataRef(dataRef) {}

    void operator=(T const & data) {*m_dataRef = data;}

protected:
    T* m_dataRef;
};

template <class T>
class Property : public PropertyImpl<T> {};

Note that TypeFamily<> is some meta-code computing whether T is a supported base type. TypeFamily<T>::value is 0 if T is a float.

Now I create an aggregated Property

// myNode.h.

class myNode
{
public:
    void setProp(float val) {m_prop = val;}

protected:
    Property<float> m_prop;
}

I was initially thinking that Property<float> deriving from PropertyImpl<float, 0> I would be able to call m_prop = val, as operator=() is defined for PropertyImpl<float, 0>. But my compiler returns the following error:

<myNode_path>(myNode_line) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'float' (or there is no acceptable conversion)
3>        <myProperty_path>(myProperty_line): could be 'Property<T> &Property<T>::operator =(const MPC::Property<T> &)'
3>        with
3>        [
3>            T=float
3>        ]
3>        while trying to match the argument list '(Property<T>, float)'
3>        with
3>        [
3>            T=float
3>        ]

This is totally unclear to me, and I have the feeling I missed a fundamental behavior of templates. Or it is an easy catch right in front of my eyes...

Anyone understands what is going on?

Thanks!

Это было полезно?

Решение

Property contains an implicitly-declared copy-assignment operator, which hides the one in the base class. You'll need a using-declaration to make it accessible:

template <class T>
class Property : public PropertyImpl<T> {
public:
    using PropertyImpl<T>::operator=;
};
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top