Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-10-31 10:50:33

From: <helmut.zeisel_at_[hidden]>
> Should the following program compile?
> GCC 3.0.1 says


> Is GCC right?

Unfortunately yes. This is an open issue (CWG #214 IIRC.)

> If yes, is there any other way
> to this partial specialization/overload?

Use the helper class template trick:

template<class Out, class In> struct numeric_cast_helper
 Out operator()(In in) const { return in; }

template<typename Out, typename In> Out numeric_cast(In in)
  return numeric_cast_helper<Out, In>()(in);

template<typename Digit>
class my_int {};

template<typename Digit> struct numeric_cast_helper<my_int<Digit>, double>
 my_int<Digit> operator()(double arg)
  return my_int<Digit>();

int main()
 double a=5;

 my_int<int> j=numeric_cast<my_int<int> >(a);

 return 0;

Peter Dimov
Multi Media Ltd.

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