numeric_limits<>::max()

  • Follow


Hello,

I'd like to know why std::numeric_limits<>::max (and some, but not all other 
values in numeric_limits) was chosen to be a function instead of just being 
a constant. I would have needed it in a template argument, but can't use it 
there, because it's not a constant expression.

0
Reply Rolf 12/28/2009 11:16:57 AM

On Dec 28, 6:16=A0pm, Rolf Magnus <ramag...@t-online.de> wrote:
> Hello,
>
> I'd like to know why std::numeric_limits<>::max (and some, but not all ot=
her
> values in numeric_limits) was chosen to be a function instead of just bei=
ng
> a constant. I would have needed it in a template argument, but can't use =
it
> there, because it's not a constant expression.

Dear Sir I have a question in atl.html the latest post I made, please
come answer, I need to learn the basic steps in that, I don't need
real life big hard software from corporation for me to join in some
team to create.
I think I begin to fall for web now and possibly long later in the
future.

About your question,
there is probably a '_max' variable that might have been implemented
as a private object for easier, safer and more efficient manipulation
later in the client code, so max is named to be used as a function. Do
you agree to that ?
0
Reply Naive 12/28/2009 1:09:00 PM


Rolf Magnus wrote:
> I'd like to know why std::numeric_limits<>::max (and some, but not all other 
> values in numeric_limits) was chosen to be a function instead of just being 
> a constant.

You should post to 'comp.std.c++' then.  There decisions and the 
rationales behind the Standard and any of its parts are discussed.  Some 
folks *here* might remember how those things (functions) came to be 
functions, of course, but 'c.s.c++' is more reliable.

 > I would have needed it in a template argument, but can't use it
> there, because it's not a constant expression.

You may need to redesign your classes to accommodate functions there. 
If you post your code and describe what you're trying to accomplish, 
perhaps somebody could make a suggestion...

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
0
Reply Victor 12/28/2009 3:59:05 PM

Victor Bazarov wrote:
> Rolf Magnus wrote:
>> I'd like to know why std::numeric_limits<>::max (and some, but not
>> all other values in numeric_limits) was chosen to be a function
>> instead of just being a constant.
>
> You should post to 'comp.std.c++' then.  There decisions and the
> rationales behind the Standard and any of its parts are discussed. 
> Some folks *here* might remember how those things (functions) came
> to be functions, of course, but 'c.s.c++' is more reliable.

I believe it was because the floating point values could not be const 
static members initialized in the class definition. So it was 
considered more consistent to make them all functions. That way, at 
least they could be inlined.

>
>> I would have needed it in a template argument, but can't use it
>> there, because it's not a constant expression.

It doesn't help you here, but in C++0x the members of numeric_limits 
will be declared constexpr, to make them usable as constant 
expressions, even though they remain functions. A new language 
feature!


Bo Persson
 


0
Reply Bo 12/28/2009 5:41:38 PM

On Dec 28, 5:41 pm, "Bo Persson" <b...@gmb.dk> wrote:
> Victor Bazarov wrote:
> > Rolf Magnus wrote:
> >> I'd like to know why std::numeric_limits<>::max (and some,
> >> but not all other values in numeric_limits) was chosen to
> >> be a function instead of just being a constant.

> > You should post to 'comp.std.c++' then.  There decisions and
> > the rationales behind the Standard and any of its parts are
> > discussed.  Some folks *here* might remember how those
> > things (functions) came to be functions, of course, but
> > 'c.s.c++' is more reliable.

> I believe it was because the floating point values could not
> be const static members initialized in the class definition.
> So it was considered more consistent to make them all
> functions.

They have to all be functions, or none, or you can't very easily
use the value in a template.

--
James Kanze
0
Reply James 12/28/2009 9:16:12 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rolf Magnus wrote:

> Hello,
> 
> I'd like to know why std::numeric_limits<>::max (and some, but not all
> other values in numeric_limits) was chosen to be a function instead of
> just being a constant. I would have needed it in a template argument, but
> can't use it there, because it's not a constant expression.

Compile your code as C++0x period.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAktAniIACgkQG6NzcAXitM8XBQCfTyYM4vn+1QqADazIwSPzhXjv
Ls4AnRIbzljlPu4bQOmBMrBwdIY+Bjf7
=cMaJ
-----END PGP SIGNATURE-----

0
Reply Michael 1/3/2010 1:39:46 PM

5 Replies
287 Views

(page loaded in 0.075 seconds)

Similiar Articles:













7/27/2012 1:51:22 PM


Reply: