Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2002-01-21 16:09:34


On Monday, January 21, 2002, at 03:37 PM, David Abrahams wrote:

> It's not clear. I think Howard wants people to be able to freely and
> reliably mix uses of functions with the same name from different
> namespaces.
> Though it would be wonderful, I have doubts that it's possible. IMO, for
> reliability, a user has to be explicit about which namespace's
> semantics he
> is implementing for a given function name.

This is something of a versioning problem. Consider that you have
shipped a library named MyLib. And because you're a careful programmer,
MyLib is in its own namespace. Your customers have responded well and
it is time to put out version 2 of MyLib. Now you know that some of
your customers would never do something like "using namespace MyLib;".
But you also know that some of your customers do. If you want to
introduce a function template for a restricted set of types, what is the
very best you can do to keep from breaking your customer's code? (even
those customers that are um... less than stellar programmers).

Example:

Say you want to introduce into your library a function called fma that
looks like:

double fma(double x, double y, double z);

And you would also like float and long double versions of this
function. And hey, since your competition (C99) allows arbitrary
arithmetic types to be used in any combination with this function, that
would be nice too. But how to implement?

First try:

Implement overloads for each combination of: int, unsigned int, long,
unsigned long, float, double, and long double. And maybe long long
types too. That's 9 types or 729 overloads (9^3). Hmm... not my first
choice.

Second try:

template <class T, class U, class V>
struct promote
{
        typedef (details) type;
};

template <class T, class U, class V>
typename promote<T, U, V>::type
fma(T t, U u, V v);

Nice compact code (compared to 729 overloads) but this fails for your
customers who like to use using directives and have written a fma for
their own types.

Third (wishful thinking) try:

Use a language feature to restrict your template parameters to
arithmetic types.

-Howard


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