Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I pass std::isfinite<double> as a predicate function?

I'm trying to pass a specific overload of std::isfinite() to a function, but GCC refuses:

0.cpp:9:24: error: no matching function for call to ‘all_of(std::array<double, 2>::const_iterator, std::array<double, 2>::const_iterator, <unresolved overloaded function type>)’
    9 |     return !std::all_of(a.begin(), a.end(), std::isfinite<double>);
      |             ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's the source:

#include <algorithm>
#include <array>
#include <cmath>

int main()
{
    auto const a = std::array<double, 2>{{0.0, 1.0}};
    return !std::all_of(a.begin(), a.end(), std::isfinite<double>);
}

Why does it consider std::isfinite<double> to be an unresolved overloaded function type, and is there a solution simpler than wrapping in a lambda function of my own? I'd prefer not to have to write [](double x){ return std::isfinite(x); } if I don't need to.

This is something I came across in some code that was previously compiled with a Microsoft compiler, but which doesn't build for me using GCC.

If it matters, I see the same symptom with all the standards versions I tried: -std=c++11, -std=c++17 and -std=c++23.

like image 538
Toby Speight Avatar asked Mar 28 '26 10:03

Toby Speight


2 Answers

Generally you cannot count on the absence of other overloads in the standard library.


This also means that functions in the standard library cannot be taken their address, unless they are explicitly marked as addressable functions.

Also for custom functions, there is no such thing as a "pointer to an overload set". In the presence of different overloads, to get a pointer you must either pick one of the overloads:

 void foo(int);
 void foo(double);
 auto ptr = &foo;                              // error
 auto ptr = static_cast<void(*)(int)>(foo);    // ok

Or defer overload resolution to when the function is actually called (see below).


From cppreference about std::isfinite:

Additional overloads are provided for all integer types, which are treated as double.

and

The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::isfinite(num) has the same effect as std::isfinite(static_cast<double>(num)).

You can wrap it inside a lambda:

std::all_of(a.begin(), a.end(),[](auto x){ return std::isfinite(x);});
like image 93
463035818_is_not_a_number Avatar answered Mar 30 '26 00:03

463035818_is_not_a_number


is there a solution simpler than wrapping in a lambda function of my own?

you cannot actually call std::isfinite<double>(5.0) as the template argument is only for Integer types.

the overload that accepts double is just an overload, not a template, you just need to cast to the correct overload.

std::all_of(a.begin(), a.end(), static_cast<bool(*)(double)>(std::isfinite));

Edit: while this works on all C++ compilers all the way to (and including) C++23, it is aperantly UB to take a pointer to a function from the standard library as highlighted in the comments.

compilers don't need to guarantee this code will compile, the lambda in the other answer is more future-proof

like image 33
Ahmed AEK Avatar answered Mar 30 '26 00:03

Ahmed AEK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!