473,387 Members | 1,637 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,387 software developers and data experts.

numeric_limits<> specialization


Hello all,

Is is allowable for me to specialize numeric_limits<> for my own numeric
type? Does this violate any sort of rule against mucking with std?

Thanks,
Dave
Jul 22 '05 #1
8 4539
On Sat, 6 Dec 2003 19:44:51 -0700, "Dave" <be***********@yahoo.com> wrote:
Is is allowable for me to specialize numeric_limits<> for my own numeric
type? Does this violate any sort of rule against mucking with std?


Technically yes, I think it does violate that rule.

Practically, no, I don't know any compiler that enforces that rule.

But you don't have to, just make your own class that forwards to
std::numeric_limits. A bit tedious, perhaps, but then you can fix some
things at the same time.

Jul 22 '05 #2

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f***************@News.CIS.DFN.DE...
On Sat, 6 Dec 2003 19:44:51 -0700, "Dave" <be***********@yahoo.com> wrote:
Is is allowable for me to specialize numeric_limits<> for my own numeric
type? Does this violate any sort of rule against mucking with std?


Technically yes, I think it does violate that rule.

Practically, no, I don't know any compiler that enforces that rule.

But you don't have to, just make your own class that forwards to
std::numeric_limits. A bit tedious, perhaps, but then you can fix some
things at the same time.


Yeah, even Comeau doesn't reject it, but performing this specialization sure
seems suspect to me! Comeau and VC++ 7.1 accept what's shown below without
so much as a warning.

Hmmm.... A class that forwards to std::numeric_limits<>... Interesting! I
assume that involves a class template named numeric_limits<> in my own
namespace (call it dave) and some sort of scheme to check if
std::numeric_limits<T>::is_specialized is true. If it is, that value is
used, otherwise I will expect that client code has specialized my
dave::numeric_limits with their numeric type. At least I think that would
be the gist of it...
#include <limits>

using namespace std;

struct foo_t
{
};

namespace std
{
template<>
class numeric_limits<foo_t>
{
public:
static const bool has_infinity = false;
};
}

int main()
{
(void) numeric_limits<foo_t>::has_infinity;
}
Jul 22 '05 #3
"Alf P. Steinbach" <al***@start.no> wrote...
On Sat, 6 Dec 2003 19:44:51 -0700, "Dave" <be***********@yahoo.com> wrote:
Is is allowable for me to specialize numeric_limits<> for my own numeric
type? Does this violate any sort of rule against mucking with std?
Technically yes, I think it does violate that rule.


What rule is that? I've not seen the work "mucking" in the Standard.
And, as a matter of fact, you _are_ allowed to place any specialisation
of a standard template in the std namespace. See 17.4.3.1/1.
Practically, no, I don't know any compiler that enforces that rule.
And they are not supposed to.
But you don't have to, just make your own class that forwards to
std::numeric_limits. A bit tedious, perhaps, but then you can fix some
things at the same time.


....
Jul 22 '05 #4
On Sun, 07 Dec 2003 04:16:35 GMT, "Victor Bazarov" <v.********@comAcast.net> wrote:
"Alf P. Steinbach" <al***@start.no> wrote...
On Sat, 6 Dec 2003 19:44:51 -0700, "Dave" <be***********@yahoo.com> wrote:
>Is is allowable for me to specialize numeric_limits<> for my own numeric
>type? Does this violate any sort of rule against mucking with std?
Technically yes, I think it does violate that rule.


What rule is that?


§17.4.3.1/1 first sentence: you're not allowed to add to namespace std,
"unless otherwise specified".
And, as a matter of fact, you _are_ allowed to place any specialisation
of a standard template in the std namespace. See 17.4.3.1/1.


I don't see the word "any" in the standard.

As a matter of fact, §17.4.3.1/1 requires that such a specialization
depends on a user-defined name of external linkage, and also has some other
requirements on the specialization, so what you write is incorrect.

But nitpicking aside, yup, your intuition is correct that the OP can probably
safely place his specialization in namespace std; my intutition about that
was incorrect.

Practically, no, I don't know any compiler that enforces that rule.


And they are not supposed to.


Yes they are, but that is quality-of-implementation issue.

Jul 22 '05 #5
Dave wrote in news:vt************@news.supernews.com:

Hello all,

Is is allowable for me to specialize numeric_limits<> for my own
numeric type? Does this violate any sort of rule against mucking with
std?


Yes you can, you must however do a complete specialization, i.e.
provide all the members and types that the genuine article provides.

The easiest way to do that (in this case) is to derive from the
nearest pre-existing specialization, so for an unsigned like type:

struct an_unsigned
{
// whatever ...
};

#include <limits>

namespace std
{
template <>
class numeric_limits< an_unsigned>
:
public numeric_limits< unsigned >
{
public:

static const bool is_specialized = true;

static const int digits = /* whatever */;
static const int digits10 = (digits * 77) / 256;
};
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

"Alf P. Steinbach" <al***@start.no> wrote in message news:3f***************@News.CIS.DFN.DE...
On Sat, 6 Dec 2003 19:44:51 -0700, "Dave" <be***********@yahoo.com> wrote:
Is is allowable for me to specialize numeric_limits<> for my own numeric
type? Does this violate any sort of rule against mucking with std?
Technically yes, I think it does violate that rule.


No it doesn't.

Practically, no, I don't know any compiler that enforces that rule.

It's not requred to. Adding things (other than template specializations)
is undefined behavior.
Jul 22 '05 #7

"Dave" <be***********@yahoo.com> wrote in message news:vt************@news.supernews.com...

Hello all,

Is is allowable for me to specialize numeric_limits<> for my own numeric
type? Does this violate any sort of rule against mucking with std?

No...specializing templates from std is one of the few things you are allowed to do:
17.4.3.1 of the standard.
Jul 22 '05 #8
On Sun, 7 Dec 2003 13:21:18 -0500, "Ron Natalie" <ro*@sensor.com> wrote:

"Alf P. Steinbach" <al***@start.no> wrote in message news:3f***************@News.CIS.DFN.DE...
On Sat, 6 Dec 2003 19:44:51 -0700, "Dave" <be***********@yahoo.com> wrote:
>Is is allowable for me to specialize numeric_limits<> for my own numeric
>type? Does this violate any sort of rule against mucking with std?


Technically yes, I think it does violate that rule.


No it doesn't.


Victor and I have already discussed this, in this thread.

Some time ago.

The more complete answer is: it might, but probably doesn't. The specialization
must fulfill all requirements that the standard places on the original template,
and it must depend on a user-defined name with external linkage. Otherwise it's
undefined behavior.
Practically, no, I don't know any compiler that enforces that rule.

It's not requred to.


That's a quality-of-implementation issue.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Jalal | last post by:
I am trying to use numeric_limits<double>::min() in an MFC application in Microsoft Visual C++.NET 2003. I am having some difficulties here. The following are the parts of a simple program I wrote...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Steve | last post by:
I don't get it. In Codewarrior for Mac OS 9.4, numeric_limits<unsigned char>::digits10 == 2. Unless I don't understand it properly (most likely!) I thought digits10 was supposed to represent...
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: ...
1
by: Jacek Dziedzic | last post by:
Hello! This is my first time dealing with Very Large Files. I have vector of strings representing numbers and I need to extract bytes in binary mode from a Large File that correspond to...
4
by: john | last post by:
The code: #include <iostream> #include <limits> int main() { using namespace std;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.