C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Question Regarding Default Behavior of std::numeric_limits<T> Primary Template

From: Simon Kraemer <sikraemer_at_[hidden]>
Date: Sun, 12 Jan 2025 11:24:16 +0100
> It's an unnecessarily complicated way to show that numeric_limits
returns zero for unknown arithmetic-like types, or a value-initialized
value for arbitrary types.

Got that.

> Using numeric_limits for non-numeric types doesn't seem like an important
use case, but it's still an odd API.

Now I got you.
AFAIK this is mostly due to historical reasons and backward compatibility.
I found this post on Stackoverflow that goes into the history of
std::numeric_limits and why it is as it is.
https://stackoverflow.com/a/47521516/4181011

The most important part to answer your question is probably this section:

*it was in the pre-standard publicly circulated drafts (~1995). Template
meta programming was invented by Erwin Unruh around the Stockholm meeting
(July 1996). At this point nobody thought whether it could be detectable
that a class template is defined. Instead,
std::numeric_limits<T>::is_specialized would indicate (at compile-time)
whether the class template is specialized and meaningful for type T.*



Am So., 12. Jan. 2025 um 11:07 Uhr schrieb Jonathan Wakely <cxx_at_[hidden]>:

>
>
> On Sun, 12 Jan 2025, 08:53 Simon Kraemer via Std-Proposals, <
> std-proposals_at_[hidden]> wrote:
>
>> I don't get your question.
>> You are calling operator() on the return value of min()/lowest()/max()
>> (see the double parenthesis?).
>>
>> This won't work for actual numerics, generally:
>> <source>:12:36: error: expression cannot be used as a function
>> 12 | std::numeric_limits<int>::min()();
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
>> <source>:13:39: error: expression cannot be used as a function
>> 13 | std::numeric_limits<int>::lowest()();
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
>> <source>:14:36: error: expression cannot be used as a function
>> 14 | std::numeric_limits<int>::max()();
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
>>
>> If you omit one of the parentheses, nothing is printed (as expected)
>> https://godbolt.org/z/o7z6M7qje
>>
>> Could you please clarify?
>>
>
> It's an unnecessarily complicated way to show that numeric_limits returns
> zero for unknown arithmetic-like types, or a value-initialized value for
> arbitrary types. Using numeric_limits for non-numeric types doesn't seem
> like an important use case, but it's still an odd API.
>
> See https://wg21.link/p1841 for a stalled proposal to replace
> numeric_limits with something better.
>
>
>
>

Received on 2025-01-12 10:24:29