extending std::numeric_limits

J

jamesrjones

I'm writing a template library that models the IEEE754(r) decimal
arithmetic standard, and I'd like to provide numeric_limits<> for my
templates. The problem is that I'm not sure how to do this.

If I had a simple (untemplated) class T, it would be easy. But what I
have instead is a model of a decimal type (call it DecimalType). Most
of my free functions, operations, etc look like this:

template <typename DecimalType>
DecimalType
operator+ (const DecimalType& lhs, const DecimalType& rhs);

A DecimalType is then supposed to have a certain interface.

So what I'd like to do is this:

namespace std {

template <typename DecimalType>
class numeric_limits
{
// extensions go here
};

}

But I can't do that because std::numeric_limits is already defined. So
I need to be able to provide a partial specialization. But how can I
do this? (Note: I can see some ways to do it if DecimalType is
required to derive from an ABC, but I'd prefer not to do this.)

Thanks,
- James
 
V

Victor Bazarov

I'm writing a template library that models the IEEE754(r) decimal
arithmetic standard, and I'd like to provide numeric_limits<> for my
templates. The problem is that I'm not sure how to do this.

If I had a simple (untemplated) class T, it would be easy. But what I
have instead is a model of a decimal type (call it DecimalType). Most
of my free functions, operations, etc look like this:

template <typename DecimalType>
DecimalType
operator+ (const DecimalType& lhs, const DecimalType& rhs);

A DecimalType is then supposed to have a certain interface.

So what I'd like to do is this:

namespace std {

template <typename DecimalType>
class numeric_limits
{
// extensions go here
};

}

But I can't do that because std::numeric_limits is already defined. So
I need to be able to provide a partial specialization. But how can I
do this? (Note: I can see some ways to do it if DecimalType is
required to derive from an ABC, but I'd prefer not to do this.)

...
template<class T, class U, class V>
class numeric_limits<DecimalType<T,U,V> > {
// extensions go here
};

V
 
J

jamesrjones

..
template<class T, class U, class V>
class numeric_limits<DecimalType<T,U,V> > {
// extensions go here

};

V

This would work fine if DecimalType were a template. But it isn't.
What I have is this (for example):

template < class T, class U >
class fast_model_of_decimals {};

template < class T, class U >
class small_model_of_decimals {};

template < class T, class U >
class other_model_of_decimals {};

I could do this:

template < class T, class U >
class numeric_limits< fast_model_of_decimals< class T, class U > > {};

.... and so on for the other models.

But what I'd like to do is something like this:

template <typename T>
class numeric_limits< T > {};

where T must be a model of a decimal type.

Note: I'm using BOOST and I'm familiar with enable_if, which works
great but doesn't quite seem to fit this application. I have a type
trait template is_decimal_type which returns true if the type is a
model of a fixed decimal type and false otherwise.

- James
 
V

Victor Bazarov

This would work fine if DecimalType were a template. But it isn't.
What I have is this (for example):

template < class T, class U >
class fast_model_of_decimals {};

template < class T, class U >
class small_model_of_decimals {};

template < class T, class U >
class other_model_of_decimals {};

I could do this:

template < class T, class U >
class numeric_limits< fast_model_of_decimals< class T, class U > > {};

... and so on for the other models.

That's what you should (or have to) do.
But what I'd like to do is something like this:

template <typename T>
class numeric_limits< T > {};

where T must be a model of a decimal type.

You cannot do that because that is not a specialisation.
Note: I'm using BOOST and I'm familiar with enable_if, which works
great but doesn't quite seem to fit this application. I have a type
trait template is_decimal_type which returns true if the type is a
model of a fixed decimal type and false otherwise.

Just bite the bullet and specialise it for each of your templates.

V
 
J

jamesrjones

You cannot do that because that is not a specialisation.

Yes, I know.
Just bite the bullet and specialise it for each of your templates.

There are a couple of reason why I want to avoid this:

1. Each specialization will have an identical implementation. (I
realize I could probably define a macro to keep the code duplication
to a minimum, but this still seems ugly.)

2. I'd like to make it easy to extend the library as easily as
possible. Currently, users can do this by (a) creating a new model and
(b) providing a new specialization of the type trait template. I can
add a (c), but the more steps that are required, the harder this is
for potential users.

Any creative ideas out there?

Thanks,
- James
 
M

mlimber

Yes, I know.


There are a couple of reason why I want to avoid this:

1. Each specialization will have an identical implementation. (I
realize I could probably define a macro to keep the code duplication
to a minimum, but this still seems ugly.)

2. I'd like to make it easy to extend the library as easily as
possible. Currently, users can do this by (a) creating a new model and
(b) providing a new specialization of the type trait template. I can
add a (c), but the more steps that are required, the harder this is
for potential users.

Any creative ideas out there?

Define a single class with the limits you want, and then make your
other specializations publicly inherit from it.

Cheers! --M
 
B

Bo Persson

Yes, I know.


There are a couple of reason why I want to avoid this:

1. Each specialization will have an identical implementation. (I
realize I could probably define a macro to keep the code duplication
to a minimum, but this still seems ugly.)

Or use a base implementation

template<>
std::numeric_limits<YourType> : base_implementation<some, parameters>
{ };


Bo Persson
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top