User defined numerical types as template parameters

Discussion in 'C++' started by ALiX, Jul 29, 2007.

  1. ALiX

    ALiX Guest

    I'm writing a class where one template parameter must be a type
    behaving like a floating-point type. Is there a standard way of
    checking for this?

    I can come up with two ways of doing this. One is to check if
    std::numeric_limits<T>::is_specialized == true and
    std::numeric_limits<T>::is_integer == false. Do other libraries use
    this test? Is this in any way standard practice?

    A second alternative I can think of is to write a FloatingPointConcept
    class and use Boost's concept_check library. But I'm not sure as to
    what syntactic constructions I should test for (seems to be many of
    them!). Also, even if a type conforms to the syntatic constructions I
    test for, the type might not behave like a floating point type
    semantically.

    Cheers,
    /ALiX
    ALiX, Jul 29, 2007
    #1
    1. Advertising

  2. ALiX

    Greg Herlihy Guest

    On Jul 29, 9:49 am, ALiX <> wrote:
    > I'm writing a class where one template parameter must be a type
    > behaving like a floating-point type. Is there a standard way of
    > checking for this?
    >
    > I can come up with two ways of doing this. One is to check if
    > std::numeric_limits<T>::is_specialized == true and
    > std::numeric_limits<T>::is_integer == false. Do other libraries use
    > this test? Is this in any way standard practice?


    Yes, the unary type trait: std::tr1::is_floating_point<> (or the boost
    version, if tr1 is not available for your compiler) would be the
    standard way of detecting any of the three built-in floating point
    types (float, double and long double) as well as any user-defined type
    with "floating point behavior" that had defined the appropriate
    is_floating_point<> specialization.

    Greg
    Greg Herlihy, Jul 29, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guilherme Pinto
    Replies:
    1
    Views:
    313
    Victor Bazarov
    Oct 10, 2003
  2. grahamo
    Replies:
    4
    Views:
    994
    Thomas Wintschel
    Feb 23, 2004
  3. Oodini
    Replies:
    1
    Views:
    1,767
    Keith Thompson
    Sep 27, 2005
  4. Replies:
    1
    Views:
    498
    Sion Arrowsmith
    Jul 10, 2008
  5. kito
    Replies:
    2
    Views:
    413
Loading...

Share This Page