Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-12-03 17:25:04


David Abrahams wrote:
> "Eric Niebler" <eric_at_[hidden]> writes:
>
>
>>David Abrahams wrote:
>>
>>>Eric Niebler <eric_at_[hidden]> writes:
>>>
>>>
>>>>Put the overload in an associated namespace of the type. Or for maximum
>>>>portability, you could put the overload at global scope.
>>>
>>>
>>>Really? If invoked from within a template, global scope overloads
>>>won't be found unless an argument is in the global namespace or the
>>>overloads happen to appear before the point of definition of the
>>>template.
>>
>>
>>That was the idea, yes.
>>
>>
>>
>>>If you want a way to make this portable, consider adding a dummy
>>>parameter that comes from namespace Boost and asking people to
>>>overload there (the Ramey trick).
>>
>>
>>I'm not sure how this helps on compilers that don't do ADL. This is
>>a macro. BOOST_FOREACH(foo, bar) will expand to something that makes
>>a non-qualified call to boost_foreach_has_cheap_copy *in the context
>>of the BOOST_FOREACH invocation*. This could be any context, in any
>>namespace.
>
>
> Right. Including from within a template, which could foil lookup in
> the global namespace.
>
>
>>If I want this to work on compilers that don't do ADL,
>>requiring people to make their overloads visible at global scope
>>seems necessary, but I've likely overlooked something.
>
>
> namespace boost
> {
> template <class T>
> struct foreach_has_cheap_copy
> {
> char (& test(...)) [2];
> char test(mpl::true_*);
> typedef mpl::bool_<
> sizeof(
> test(
> boost_foreach_has_cheap_copy(
> (T*)0,
> boost::whatever()) ))
> > type;
> };
> }
>
> Now all compilers will look in namespace boost, whether or not they
> support ADL.
>

I thought of something like this shortly after I sent the email. Sadly,
I cannot get VC6 to eat it. "error C2133: '$S38' : unknown size". Pbbth.
I tried a few variations, and then gave up.

Templates don't "foil lookup in the global namespace." They merely
require that the overload has been seen already. I have no problem
requiring users of broken compilers to put the overload where
BOOST_FOREACH can find it. Users of non-broken compilers can put the
overload in an associated namespace and be happy.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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