Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-06-18 05:43:27


Gennadiy Rozental wrote:
> Hi,
>
> I am having problems with subject test with Metrowerks compiler. I
> was able to minimize the issue to the following snippet:
> #include <list>
> #include <iostream>
>
> template<typename T>
> inline void
> print( std::ostream& ostr, T const& t, long ) { ostr << t; }
>
> template<typename T>
> inline void
> moo( std::ostream& ostr, T const& t ) { print( ostr, t, 0 ); }
>
> inline void
> print( std::ostream& ostr, std::list<int> const& t, int ) {}
>
> void foo()
> {
> std::list<int> lst;
>
> print( std::cout, lst, 0 ); //1
> moo( std::cout, lst ); //2
> }
>
> Line 1 compile successfully, while line 2 chokes.
>
> Any Ideas?

Yes, the code is broken. moo doesn't see the second overload. Templates do
argument-dependent lookup from the point of instantiation, and since the
only namespace associated with the 'print(ostr, t, 0)' call is 'std', the
second print is not found. You need to move it before the definition of moo
(or place it in std:: which is oddly evil but right.)

> P.S. Rest of Windows compilers I have access to seems to be happy with
> above.

The compilers are broken, too. They don't implement two phase lookup in
templates.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk