[concept_check] Restricting to floating point arguments
Hi there, I need to make sure that a given template class is only instantiated with integer types, and another one only with the likes of float or double. I have played with boost::concept_check and was wondering, why it is possible to say BOOST_CONCEPT_ASSERT((Integer<T>)); while there is no predefined BOOST_CONCEPT_ASSERT((FloatingPoint<T>)); The simple code below seems to do its job, so there must be some trade-off which I do not see ? /*************************************************************************/ #include <iostream> #include <boost/cstdint.hpp> #include <boost/concept_check.hpp> #include <boost/concept/usage.hpp> #include <boost/concept/detail/concept_def.hpp> /*****************************************************************/ namespace boost { BOOST_concept(FloatingPoint, (T)) { BOOST_CONCEPT_USAGE(FloatingPoint) { x.error_type_must_be_a_floating_point_type(); } private: T x; }; template <> struct FloatingPoint<long double> {}; template <> struct FloatingPoint<double> {}; template <> struct FloatingPoint<float> {}; } /*****************************************************************/ template <class T> class integerWrapper { BOOST_CONCEPT_ASSERT((boost::Integer<T>)); public: integerWrapper() { /* nothing */ } }; /*****************************************************************/ template <class T> class fpWrapper { BOOST_CONCEPT_ASSERT((boost::FloatingPoint<T>)); public: fpWrapper() { /* nothing */ } }; /*****************************************************************/ main() { integerWrapper<boost::int32_t> iW; fpWrapper<double> fW_double; // o.k. fpWrapper<float> fW_float; // o.k. // fpWrapper<char> fW_char; // fails to compile. Good. // fpWrapper<boost::int32_t> fW_int32; // fails to compile. Good } /*************************************************************************/ Best Regards, Ruediger
I need to make sure that a given template class is only instantiated with integer types, and another one only with the likes of float or double.
I have played with boost::concept_check and was wondering, why it is possible to say
You could also use the type_traits is_integral and is_floating_point in conjunction with either enable_if or a static assert. HTH, John.
participants (2)
-
John Maddock
-
Ruediger Berlich