|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2004-02-24 04:15:55
Guillaume Melquiond wrote:
> How did you deal with occurrences of min and
> max that rely on ADL (sorry if that is not the correct term)? Or did you
> handle only explicitly qualified calls std::min and std::max?
>
> I'm asking because the Interval library relies on this kind of construct
> in order to deal with built-in types (like int, float, etc) and
> user-defined types (their min and max are found by Koenig lookup).
Hmmm ... I was aware of the theoretical possibility of this problem, but
I didn't know there was actual boost code relying on ADL with
std::min/std::max. And the regression test with VC7.1 didn't expose this
problem. (Shortcoming of vc7.1 or the interval regression tests?)
Getting the interval library to play nice with the min/max macros will
take some work. Considering your code:
template< class T >
void f(T const &a, T const &b) {
using std::min; min(a,b);
}
It *might* compile in the presence of the min() macro but it would
likely do the wrong thing. It's unfortunate in this case that (min)(a,b)
turns off ADL. (Out of curiosity, is there a case where it's not
unfortunate? Ever since this language "feature" was brought to my
attention last year, I have wondered about it.)
The obvious answer is to #undef min and max for the interval library, or
at least #pragma push_macro/pop_macro for the compilers that support
that. Yech.
Another thought just occured to me. Consider:
#define BOOST_EMPTY
template< class T >
void f(T const &a, T const &b) {
using std::min; min BOOST_EMPTY (a,b);
}
This seems to be enough to prevent min() macro substitution, and it
would preserve ADL. I checked with VC6, VC7.1, gcc (cygwin) and with
Comeau Online, and they all like it. Will this work on other compilers too?
-- 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