Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-10-31 09:17:07


From: <helmut.zeisel_at_[hidden]>
> I want to specialize numeric_cast for a user definied class.
>
> GCC 3.0.1 compiles the following code,

GCC sees a different version of the code.

> Visual C++ 6.0 says
>
> example_numeric_cast.cpp(20) : error C2572: 'numeric_cast' :
> redefinition of default parameter : parameter 2
> example_numeric_cast.cpp(19) : see declaration of 'numeric_cast'
>
> Is the code correct?

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);
                        ^

> If yes, what is a suitable work around for VC++?

The best workaround is to remove BOOST_EXPLICIT_TARGET from cast.hpp
altogether. MSVC 6 (latest SP) has no problems with the following:

#include <iostream>
#include <typeinfo>

template<typename Target, typename Source> inline Target numeric_cast(Source
arg)
{
 std::cout << "numeric_cast<" << typeid(Target).name() << ">(" <<
typeid(arg).name() << ")\n";
 return Target(arg);
}

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

BOOST_EXPLICIT_TARGET-type workarounds are only needed when the template
parameter is not referenced at all, even in the return type.

--
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