I’m getting a linker error when using a template class where I tried implementing the copy-and-swap idiom as suggested here:
What is the copy-and-swap idiom?
The template class, let’s call it “TemplateClass” is partially defined like this:
template< class T >
class TemplateClass
{
// ...
TemplateClass< T >& operator= ( TemplateClass< T > other );
friend void swap( TemplateClass< T >& first, TemplateClass< T >& second );
// ...
};
I’ve put the implementations in a separate TemplateClass.cpp which is included in the .h file.
(Edit: I have the same issue if everything is in the .h file)
The assignment operator is defined as:
template< class T >
TemplateClass< T >& TemplateClass< T >::operator= ( TemplateClass< T > other )
{
// copy-and-swap idiom
swap( *this, other );
return *this;
}
and the swap method is defined as:
template< class T >
void swap( TemplateClass< T >& first, TemplateClass< T >& second )
{
using namespace std;
swap( first.member1, second.member1 );
swap( first.member2, second.member2 );
// ...
}
(Don’t worry, I do not really name my members “member1” etc)
I have a similar class which is defined in the same way, but is not a template class. Everything works fine there.
However, if I have a class TestClass which has a member TemplateClass< HandledClass > member and I make a call in one of its methods like
void TestClass::setMember( TemplateClass< HandledClass > newObject )
{
member = newObject;
}
I get an unresolved external error:
LNK2019: Unresolved external symbol “void __cdecl swap( class TemplateClass &, class TemplateClass &)” (…) in function “public: class TemplateClass X & __thiscall TemplateClass X::operator=(class TemplateClass)” (…) in TestClass.obj
Or in other words:
Something in TestClass calls TemplateClass<HandledClass>::operator= which does not find void swap( TemplateClass<HandledClass>, TemplateClass<HandledClass> ).
So my question is:
Why does the operator not find the swap method?
It looks like it was not compiled for the template argument . Is it somehow possible to have the compiler compile friend voids as well?
I could probably ditch the friend void approach and define an in-class swap method plus an out-of-class swap method plus one in std namespace, but I don’t know if it would work that way and I’d like to avoid that if possible anyway.
Solution:
this did the job:
template< class t >
class TemplateClass
{
friend void swap( TemplateClass& first, TemplateClass& second )
{
// ...
}
};
Note how i had to remove the < T > occurrences as well.
This is a common problem when befriending non-member functions with templates. The
frienddeclaration inside theTemplateClassdoes not befriend yourswaptemplate, but rather a non-templated free functionswapthat takesTemplateClass<T>for which everTthe template is instantiated (i.e. the specializationTemplateClass<int>will befriend a free functionvoid swap( TemplateClass<int>&,TemplateClass<int>& );that is not templated).The best solution is to provide the
swapdefinition inlined inside the class template definition, as that will make the compiler generate a non-templatedswapfunction for the exact type whenever needed. As another positive side effect, thatswapfunction will only be found during Argument Dependent Lookup, so it will not take part of overload resolution for anything that does not involve your template.Other alternatives are befriending the whole
swaptemplate function, or befriending the particular specialization of theswapfunction when applied to the sameTthat the template has been instantiated with. The first of the options is simple in code, but it grants access to all of the specializations of theswaptemplate, and that might have bad side effects. Befriending the particularswapspecialization solves that issue, but is a bit more complex to implement (you need to forward declare the class template, then theswaptemplate, then define the class template, and finally define theswaptemplate).More on this in this other answer, where the different options and syntaxes are explained with more detail.
As to the particular error message of
unresolved external, that is due to how identifier lookup works. When you usedswap(*this,other);inside a member function, lookup starts inside the class, and tries to find an appropriateswap. It first looks in the class context and finds the declaration of thefriendfree function, so lookup does not continue going outwards and adds a dependency to that particular free function. It adds the dependency and waits for the linker to locate the appropriate symbol. Because the compiler never considered the templatedswapat namespace level, it never actually instantiated it, but even if it had instantiated that template, the dependency inside theoperator=member function is on a free function, not that specialization.