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
.
3
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.
return !std::all_of(a.begin(), a.end(), static_cast<bool(*)(double)>(std::isfinite));
1
Generally you cannot count on the absence of other overloads in the standard library.
From cppreference:
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 asstd::isfinite(static_cast<double>(num))
.
You can wrap it inside a lambda:
std::all_of(a.begin(), a.end(),[](auto x){ return std::isfinite<double>(x);});
1