operator = defined using template?

O

Ondrej Spanel

I defined operator = using template member function for a template class.
However compiler failed to recognize it is defined and created its own
version (which was member-wise copy, and a result was disastrous and hard to
debug bug, as pointer was copied).

Adding explicit operator = helped. See code snip below.

I am not sure if this compiler behaviour is standard conformant or not. Any
thoughts?

Regards
Ondrej

--
---------------------------------------
Ondrej Spanel
Lead Programmer
Bohemia Interactive Studio
www.bistudio.com
www.flashpoint1985.com

----------------------------------------------------------------------------
---

template<class Type, class Allocator=MemAllocD>
class AutoArray
{
Type *_data;
int _size;
....
public:
/// template "operator ="
template <class AnotherAllocator>
void operator = ( const AutoArray<Type,AnotherAllocator> &src )
{
... deep copy of _data array
}

/// explicit "operator =" - class does not compile well without it
void operator = ( const AutoArray &src )
{
... deep copy of _data array
}
};

void Foo()
{
AutoArray< SomeTemplate<SomeClass> > a;
AutoArray< SomeTemplate<SomeClass> > b;

b = a; /// bad code generated when explicit "operator =" not defined.
}
 
D

Doug Harrison [MVP]

Ondrej said:
I defined operator = using template member function for a template class.
However compiler failed to recognize it is defined and created its own
version (which was member-wise copy, and a result was disastrous and hard to
debug bug, as pointer was copied).

Adding explicit operator = helped. See code snip below.

I am not sure if this compiler behaviour is standard conformant or not. Any
thoughts?

According to the standard, a template assignment operator is never a copy
assignment operator and doesn't suppress generation of one by the compiler.
(Ditto for copy ctors.) So it sounds like the compiler was doing the right
thing, and you solved your problem in the right way.
 
J

John Madsen

Ondrej Spanel said:
I defined operator = using template member function for a template class.
However compiler failed to recognize it is defined and created its own
version (which was member-wise copy, and a result was disastrous and hard to
debug bug, as pointer was copied).

This is standard conforming behavior. The compiler will generate a
copy assignment operator if it does not find a user-defined
non-template copy assignment operator (cf. 12.8/9 footnote 109):

"Because a template assignment operator is never a copy assignment
operator, the presence of such a template does not suppress the
implicit declaration of a copy assignment operator. Template
assignment operators participate in overload resolution with other
assignment operators, including copy assignment operators, and a
template assignment operator may be used to assign an object if it
provides a better match than other assignment operators."

John
 
O

Ondrej Spanel

I am not sure if this compiler behaviour is standard conformant or not.
Any
According to the standard, a template assignment operator is never a copy
assignment operator and doesn't suppress generation of one by the compiler.
(Ditto for copy ctors.) So it sounds like the compiler was doing the right
thing, and you solved your problem in the right way.

Thank you for your explanation.

Regards
Ondrej
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top