Operator = (T * r) in nested templates
I have a problem with nested templates and overriding the assignment operator. Let's say I want the refcounting _reference class template. For now, this just contains a pointer to the object counted by ref. The problem is that it all works great as long as I do it with simple classes or structs. eg. _reference ...,
But now I want to create a class template that is a reference to a std vector that redirects the class it contains.
No, I just posted the code: (it doesn't refcounting and this stuff right now, its just fetching the problem I have)
template <typename T>
class _reference
{
private:
T* p_;
public:
// !!! this assignment seems only to work, when T is no class template already...
void operator= (T* r)
{
p_ = r;
}
// WHILE this ALWAYS works as well...
void simplySetIt (T* r)
{
p_ = r;
}
};
template <typename T>
class _ref_vector : public _reference<vector<T> >
{
};
void test2 ()
{
_reference<vector<long> > ref_ptr2;
_ref_vector<long> ref_ptr3;
ref_ptr2 = new vector<long>; // works fine.
ref_ptr3 = new vector<long>; // BUT: THIS doesnt work
ref_ptr3.simplySetIt (new vector<long>); // WHILE: this works fine...
}
MSVC error:
error C2679: binary '=' : no operator found which takes a right-hand operand of type
'std::vector<_Ty> *' (or there is no acceptable conversion)
GCC error:
error: no match for 'operator=' in 'ptr3 = (((const stlp_std::allocator<long int>&)
((const stlp_std::allocator<long int>*)(& stlp_std::allocator<long int>()))),
(((stlp_std::vector<long int, stlp_std::allocator<long int> >*)operator new(12u)),
((<anonymous> != 0u) ? (<anonymous>->stlp_std::vector<_Tp, _Alloc>::vector [with
_Tp = long int, _Alloc = stlp_std::allocator<long int>]
(<anonymous>), <anonymous>) : <anonymous>)))'
So please, can someone explain to me why the assignment operator doesn't work here, but the simpleSetIt function is?
source to share
The base operator = is hidden by implicit assignment operators so that it no longer participates in overloading. You need to write _ref_vector
like
template <typename T>
class _ref_vector : public _reference<vector<T> >
{
using _reference<vector<T> >::operator=;
};
Since there is no compiler-added version of simpleSetIt, lookup will find it in the base class.
source to share