Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-10-31 10:38:43


From: <helmut.zeisel_at_[hidden]>
> --- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> >
> > This minimal example:
> >
> > template<class T> void f(T = 0);
> > template<> void f(int = 0);
> >
> > is not accepted by the online Comeau:
> >
> > "11110.c", line 2: error: default argument is not allowed
> > template<> void f(int = 0);
> > ^
>
> Is this just because the standard say it that way,
> or is there any other reason for that?

The standard, in 14.7.3/21. When the default argument is removed, it
compiles.

Looking into the problem in more detail, this example:

#include <iostream>

template<class T, class S> T numeric_cast(S arg)
{
 std::cout << "generic\n";
 return T(arg);
}

template<> double numeric_cast(int arg)
{
 std::cout << "specialized\n";
 return arg;
}

int main()
{
 numeric_cast<double>(5);
 return 0;
}

is accepted by MSVC 6 (and Comeau), which prints "specialized", as expected.

Both bcc32 5.5.1 and g++ 2.95.3 don't like

template<> double numeric_cast(int arg)

preferring

template<> double numeric_cast<double>(int arg)

instead. With this fix, they accept the code and print "specialized."

MSVC 7b2 accepts either version but prints "generic"! This looks like a bug.

Your original example with '= 0' removed from the explicit target gives
another error on MSVC 6 on:

boost::myint a = boost::numeric_cast<boost::myint>(0.5);

error C2660: 'numeric_cast' : function does not take 1 parameters

> > The best workaround is to remove BOOST_EXPLICIT_TARGET from cast.hpp
> > altogether.
>
> So I should ask the author of the library (Kevlin Henney)
> to do that?

Yes, although this kind of change is better suited for a major release, not
1.25.1.

--
Peter Dimov
Multi Media Ltd.

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