Search code examples
c++windowsvisual-studio-2019cmath

How to resolve "fpclassify': ambiguous call to overloaded function


I am as new to C++ as someone can be. I have been given the task of installing a simulation, but I keep running into error codes. I was able to resolve most of them, but there is one I am not sure how to tackle.

The error code is C2668, and its description is:

"fpclassify': ambiguous call to overloaded function

Project is "Run Simulation" and File is corecrt_math.h on line 415.

Honestly, I'm not sure if any of the information I have given is of any use, and I'm not sure what information to provide. Maybe it would be better if you could ask me some questions and I can answer them to the best of my ability?

I have included a screenshot of my Visual Studio 19:

Visual Studio

(Click image to enlarge)

Reproducible example (demo)

#include <cmath>

int main() {
    std::isnan(1);
}

Expected outcome: That it compiles.


Solution

  • You probably fed std::fpclassify an integer somehow. Visual Studio has an issue with the integer overloads for the <cmath> functions that manifests itself like in your case instead of casting the integer to a double in accordance with:

    c.math.syn#2.2:

    […] if any argument of arithmetic type corresponding to a double parameter has type double or an integer type, then all arguments of arithmetic type corresponding to double parameters are effectively cast to double.

    I wrote an error report for std::signbit but it's the same for all <cmath> functions that I've tested and std::fpclassify is one of them - and it's used internally by many other cmath functions.

    Line 415 in corecrt_math.h is within the isnan function which calls fpclassify internally.

    Steps to fix the problem:

    • When you build your project you'll get a list of errors in the Error List box. Look for lines showing see reference to function template instantiation 'bool isnan<int>(_Ty) noexcept' being compiled or similar. The <int> part may be any integer type.
    • Double click that line and the IDE should place the cursor on the call to isnan that is made with an integer.
    • Replace the isnan(integer) call with isnan(static_cast<double>(integer)).
    • Repeat these steps for any other cmath functions causing problems.

    Note: Using isnan with integers is pointless. isnan(integer) will always return false so a compiler with optimization turned on should replace the whole call with false.