473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

extending std::numeric_limits

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
6 2898
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher | last post by:
I am trying to calculate a number of sums a double can hold with a max addend of x. I performed the following computation: cout<<std::numeric_limits<double>::max()<<endl;...
3
by: puzzlecracker | last post by:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); I have seen it in faq - what does it do, exactly?
1
by: liam_herron | last post by:
Does anyone know why this is? Is there a compile flag to have this return 'inf' as opposed to zero? Any help would be much appreciated. I have included a sample program with sample output: ...
5
by: aaragon | last post by:
Hi everyone, I wrote a very simple function to try to understand the casting of variables in C++. The function is function foo() { std::vector<inttest(100); randomize(test); unsigned long...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.