User defined numerical types as template parameters

A

ALiX

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
 
G

Greg Herlihy

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top