|
Boost Users : |
Subject: Re: [Boost-users] [Concept] boost::FloatingPoint<T>
From: Ruediger Berlich (ruediger.berlich_at_[hidden])
Date: 2010-09-20 02:04:13
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
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