Boost logo

Boost :

From: Rozental, Gennadiy (gennadiy.rozental_at_[hidden])
Date: 2003-06-18 15:09:56


> -----Original Message-----
> From: Peter Dimov [mailto:pdimov_at_[hidden]]
> Sent: Wednesday, June 18, 2003 6:43 AM
> To: Boost mailing list
> Subject: Re: [boost] test_tools_test Metrowerks failure
>
>
> 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.

All above symbols in fact are in a some namespace. It does not have a
difference (if that what you mean)

> You need to move it before the definition of moo
> (or place it in std:: which is oddly evil but right.)

No. this wouldn't work for me.

In fact I found the solution (I think):

#include <list>
#include <iostream>

/////////////////////////////////////////////
// Library code

template<typename T>
struct printer {
void
operator()( std::ostream& ostr, T const& t )
{ ostr << t; }
};

template<typename T>
inline void
moo( std::ostream& ostr, T const& t ) { printer<T>()( ostr, t ); }

/////////////////////////////////////////////
// Users specialization

template<>
struct printer<std::list<int> > {
void
operator()( std::ostream& ostr, std::list<int> const& t )
{}
};

/////////////////////////////////////////////
// Usage code

void foo()
{
    std::list<int> lst;

    moo( std::cout, lst ); //2
}

Above works with online Cameau. Seems strange that PS and overloading works
different.

Gennadiy.


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