Search code examples
c++overloading

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.


Solution

  • 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);});