Boost logo

Boost :

From: Todd Greer (tgreer_at_[hidden])
Date: 2000-03-23 10:01:48

Mark Borgerding <mborgerding_at_[hidden]> writes:

> 2) template versions of assignment and copy construction functions must be
> defined before the non-template versions. -- ( weird ) compile error

Thanks, this may explain a problem I had a while ago.

> 4) function templates that do not have an argument that tells the compiler
> which specialization to use cannot be reliably called, -- RUNTIME error
> e.g.
> template <class T> T* makenew() {return new T();}
> template <> int * makenew<int>() {return new int(0);}
> If I were to explicitly call
> int * pI = makenew<int>();
> I believe the generic version would be called.

There is a workaround for this. Only the args get involved in name
mangling, so the arg set needs to be uniq for each specialization.
Thus the following is the way to declare an argless function template:

template <class T> T* makenew(T* = 0) { return new T(); }

I don't know whether or not the compiler is capable of optimizing the
useless paramater away. You don't need to do any explicit
specializations to tickle this bug; it appears if you use an
un-explicitly-specialized template function with different types. You
can now call makenew as you would expect:

std::auto_ptr<int> p = makenew<int>();

I believe this bug and workaround are just as relavent for member
template functions.

One odd problem I had a while ago was overloading between a template
and a non-template member func. The template took any type, and the
non-template took a const foo&. MSVC called the template version when
called with a foo. As a guess, I made the non-template member
function take a foo, and MSVC claimed ambiguity. It looks like it
uses implicit specializations in overload resolution, which I don't
think its supposed to. I suspect this would have worked the same had
they been non-member template functions.

AFAIK, the problems with member template functions are fairly well
characterized by the items that have been posted to this list, so I
think people can use member template functions for MSVC if they're
willing to support a broken compiler. (I'd certainly appreciate it.)
I use member template functions in MSVC, and I'm able to make things

Todd Greer	<tgreer_at_[hidden]>

Boost list run by bdawes at, gregod at, cpdaniel at, john at