Re: A change from 2002 to 2005 wrt to global templatized operators?
"Dean Roddey" <droddey@charmedquark.com> wrote in message
news:eSfudpB4GHA.4764@TK2MSFTNGP05.phx.gbl
template <class T> class TFundStack : public TObject
{
public :
.......
protected :
friend TBinOutStream& operator<<
(
TBinOutStream& strmOut
, const TFundStack<T>& fcolToStream
);
This befriends a non-template function.
// In the same header, global streaming operators
template <class T> TBinOutStream&
operator<<(TBinOutStream& strmToWriteTo, const TFundStack<T>&
colToStream) {
}
This is a template function, and it is _not_ declared a friend of
TFundStack. If you want to befriend this one, the correct declaration
would be
friend TBinOutStream& operator<< <T>
(
TBinOutStream& strmOut
, const TFundStack<T>& fcolToStream
);
Note that, for this to work, operator<< <T> template must be at least
forward-declared before TFundStack.
The compiler doesn't find the global operators and I get an unresolved
symbol error on operator<<(TBinOutStream&,
TFundStack<tCIDLib::TCard4>), so it's not making the connection from
the streaming of the stack to the provided global streaming operator.
Do you get compiler error, or linker error? I suspect the latter. You
see, your friend declaration also serves as a declaration for the
non-template operator<<. So now the compiler believes such a
non-template function exists. During overload resolution, the compiler
prefers non-templates over templates. But of course the non-template
version is not implemented anywhere.
Is there something I'm missing here? Is this no longer valid in C++
or is this some quirk of VC++ 2005, or is there some new switch I
need or some such thing?
Your code was not valid, and was compiling on VC2002 by accident. The
compiler bug that allowed the code to compile has been fixed. Now you
need to fix your code.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925