By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

extending std::numeric_limits

P: n/a
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

Mar 7 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ja*********@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 7 '07 #2

P: n/a
On Mar 7, 10:12 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
..
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

Mar 7 '07 #3

P: n/a
ja*********@gmail.com wrote:
On Mar 7, 10:12 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>>
..
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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 7 '07 #4

P: n/a
On Mar 7, 11:35 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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

Mar 7 '07 #5

P: n/a
On Mar 7, 11:50 am, jamesrjo...@gmail.com wrote:
On Mar 7, 11:35 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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?
Define a single class with the limits you want, and then make your
other specializations publicly inherit from it.

Cheers! --M

Mar 7 '07 #6

P: n/a
ja*********@gmail.com wrote:
On Mar 7, 11:35 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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.)
Or use a base implementation

template<>
std::numeric_limits<YourType: base_implementation<some, parameters>
{ };
Bo Persson
Mar 7 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.