Boost logo

Boost Users :

Subject: [Boost-users] [concept_check] Restricting to floating point arguments
From: Ruediger Berlich (ruediger.berlich_at_[hidden])
Date: 2010-07-02 08:05:30


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


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