From what I understand, this means that I am relying on implementation
Hi there, I'd like to make sure that some of my classes are only used with particular value types. This is easy enough to implement for integers, using BOOST_CONCEPT_ASSERT((boost::Integer<T>)) . However, there is no boost::FloatingPoint<T>, which is a bit odd. The following, easy code seems to do what I want, but reqires inclusion of 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> {}; } /*****************************************************************/ details with the above code. Is there any way to do the above in a portable way ? Thanks and Best Regards, Ruediger P.S.: If the above seems acceptable, please feel free to include it in Boost.Concept.
On Wed, Sep 15, 2010 at 2:02 PM, Ruediger Berlich <ruediger.berlich@iwr.fzk.de> wrote:
Hi there,
I'd like to make sure that some of my classes are only used with particular value types. This is easy enough to implement for integers, using BOOST_CONCEPT_ASSERT((boost::Integer<T>)) . However, there is no boost::FloatingPoint<T>, which is a bit odd.
The following, easy code seems to do what I want, but reqires inclusion of boost/concept/detail/concept_def.hpp
/*****************************************************************/
namespace boost { BOOST_concept(FloatingPoint, (T))
From what I understand, this means that I am relying on implementation details with the above code.
Yes. Why did you use the undocumente BOOST_concept macro? That only exists for backward compatibility with older uses of the BCCL.
Is there any way to do the above in a portable way?
// untested #include <boost/type_traits/is_floating.hpp> #include <boost/mpl/assert.hpp> template <class T> struct FloatingPoint { BOOST_MPL_ASSERT((boost::is_floating<T>)); };
P.S.: If the above seems acceptable, please feel free to include it in Boost.Concept.
If what I suggested works out for you, would you mind submitting a patch to Trac? -- Dave Abrahams BoostPro Computing http://www.boostpro.com
Dear Dave, thanks a lot! Dave Abrahams wrote:
On Wed, Sep 15, 2010 at 2:02 PM, Ruediger Berlich [...]
From what I understand, this means that I am relying on implementation details with the above code.
Yes. Why did you use the undocumente BOOST_concept macro? That only exists for backward compatibility with older uses of the BCCL.
This is an almost verbatim copy of the code used for Integer<T>, SignedInteger<T> etc. in $BOOSTROOOT/include/boost/concept_check.hpp .
Is there any way to do the above in a portable way?
// untested #include <boost/type_traits/is_floating.hpp> #include <boost/mpl/assert.hpp>
template <class T> struct FloatingPoint { BOOST_MPL_ASSERT((boost::is_floating<T>)); };
P.S.: If the above seems acceptable, please feel free to include it in Boost.Concept.
If what I suggested works out for you, would you mind submitting a patch to Trac?
The following example, using your suggestion, works for me (g++ 4.4.3 / Ubuntu 10.04). See further below for comments regarding a patch and trac item: //------------------------------------------------------------------------ #include <iostream> #include <boost/cstdint.hpp> #include <boost/concept_check.hpp> #include <boost/type_traits/is_floating_point.hpp> #include <boost/mpl/assert.hpp> /*****************************************************************/ namespace boost { template <class T> struct FloatingPoint { BOOST_MPL_ASSERT((boost::is_floating_point<T>)); }; } /*****************************************************************/ 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; fpWrapper<float> fW_float; // fpWrapper<char> fW_char; // fails to compile, just as it should // fpWrapper<boost::int32_t> fW_int32; // fails to compile, just as it should } //------------------------------------------------------------------------ It does appear, though, as if the better solution for me would be to directly use BOOST_MPL_ASSERT in my code, of which I wasn't aware prior to your answer. I've been looking all along for a something that lets me use type traits to check the types of an entire template class (instead of just single functions, as I've done previously with enable_if). As to the patch: I'm happy to do this for concept_check.hpp . Kind Regards / Mit freundlichen Gruessen, Ruediger Berlich
participants (2)
-
Dave Abrahams
-
Ruediger Berlich