Boost logo

Boost :

From: Simon Buchan (simon_at_[hidden])
Date: 2005-09-23 01:18:40


Dan McLeran wrote:
>>let's finally do it - C++ lacks bounded types and it's high time to fix
>>that! ;-)
>
>
> I agree. I had been programming in C++ for a number of years before I was
> introduced to Ada. I loved the strict typing one can do in Ada and I would
> like to have at least some of these facilities in C++ as well (since that is
> what I use every day). I will certainly check out what you've posted
> (bounded types). One thing to keep in mind as we move forward with this is
> that, IMO, we need to support as much compile-time computation as much as
> possible. One thing I am struggling with is how to support both bounded
> floating-point types, but as the same time, have a bounded types template be
> able to be used from boost::mpl for compile-time computation.
>
> Regards,
>
> Dan McLeran
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>
AFAIK, no compiler actually supports compile-time floating-point
compilation, since there is no general guarantee that the same
architecture would be running the program (think cross-compiles), and
almost every floating-point operation is "implementation-defined",
basicly, could do anything at all.
Say: PPC has 128-bit long doubles, x86 only has 80-bits, but x86
internally always uses long doubles for calculations, even with 32-bit
floats, and therefore gives different results than the PPC. I can't
remember if those statements are accurate, but something like that goes on.

You could try a factory function taking constant T's, hoping the
compiler can constant-propagate through function calls (since it would
be inline, it should be able to), and overload that for integral
expressions? (enable_if<> should be useful here) ie:

template <typename T>
class bounded_t
{ // just a boring run-time checked version
}

template <int min, int max>
class bounded_t<int>// and char, (un)signed char, wchar_t, etc...
{ // fancy compile-time version
}

template <typename T>
bounded_t<T> bounded(const T& t, const T& min, const T& max, int dummy =
disable_if<mpl::is_integral<T>, int>)
{ return bounded<T>(t, min, max);
}

template <typename T>
bounded_t<T, int, int> bounded(const T& t, const T min, const T max, int
dummy = enable_if<mpl::is_integral<T>, int>)
{ return bounded_t<T, min, max>(t);
}

BTW, I have NO idea whether anything like that would ever work, on any
far-out plane of the universe. You have been warned.


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