Boost logo

Boost Users :

Subject: Re: [Boost-users] template type asserting
From: Joachim Faulhaber (afojgo_at_[hidden])
Date: 2011-01-25 12:32:56


2011/1/25 Joachim Faulhaber <afojgo_at_[hidden]>:
> 2011/1/25 Kraus Philipp <philipp.kraus_at_[hidden]>:
>> Hello,
>>
>> I have written a template class and I would like tot "assert the type". My
>> class should only use floating point types like double or float. Can I do
>> this check with BOOST_STATIC_ASSERT  or anything else? The class need not to
>
>
> E.g.
> #include <boost/static_assert.hpp>
> #include <boost/type_traits/is_floating_point.hpp>
>
> template<class Type>
> class real_holder
> {
> public:
>    real_holder(Type val): _value(val)
>    {
>        BOOST_STATIC_ASSERT((is_floating_point<Type>::value));
>    }
> private:
>    Type _value;
> };
>
> ...
> {
>    real_holder<double> rh_d(1.0); //OK.
>    real_holder<int> rh_i(1); // compiletime error:
> //boost::STATIC_ASSERTION_FAILURE<x>
> }
>
>
>> use a "countable type" (math: in N).
>
> may be you need a different trait depending on the exact specification
> of your type.

You could use

#include <boost/detail/is_incrementable.hpp>
...
boost::detail::is_incrementable<Type>

for countability, but this also includes pointers, iterators and other
incrementable types. If you need to be more precise you can combine
those traits using boost::mpl.

HTH
Joachim

-- 
Interval Container Library [Boost.Icl]
http://www.joachim-faulhaber.de

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net