Re: Declaring war on macros

From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 04/30/04


Date: Fri, 30 Apr 2004 20:26:33 GMT


"Alf P. Steinbach" <alfps@start.no> wrote in message

> Googled on "Andrei Alexandrescu max" and there it was second in the list,
> <url: http://www.cuj.com/documents/s=7996/cujcexp1904alexandr/>.

<Quote>
As Scott notes, adding a second version:
template <class T>
T& min(T& lhs, T& rhs)
{
    return lhs < rhs ? lhs : rhs;
}
still won't work satisfactorily because the compiler won't be able to figure
out mixed cases - one const and one non-const argument.
</Quote>

I don't understand this. Why would you call min with one argument const and
the other not. What should min return -- the T& or const T&?

<Quote>
Furthermore, templates don't play nicely with automatic conversion and
promotions, which means that the following code won't compile:

int a;
short int b;
...
int smallest = min(a, b); // error: can't figure out T
                          // in template instantiation
Needless to say, the macro-based min works nicely again with such
conversions
</Quote>

That's OK usually. Usually I like the type safety std::min.

Incidentally, Andrei lists a traits mechanism by which short + long ==>
long, etc. This is one place where typeof would be nice.



Relevant Pages