Search code examples
c++11armnandefault-valuekeil

In Keil MDK-ARM C++11 *default NAN value* for member and function's parameter generates strange errors


Have you ever faced with yet another Keil's "feature" when next code

#include <cmath>
struct AB {
  float f=NAN;
  float foo(float a=NAN);
};

generates next build output:

compiling main.cpp...
..\..\src\main.cpp(24): error:  #65: expected a ";"
    float foo(float a=
AN);
..\..\src\main.cpp(24): error:  #109: expression preceding parentheses of apparent call must have (pointer-to-) function type
        float foo(float a=NAN);
..\..\src\main.cpp(24): error:  #18: expected a ")"
        float foo(float a=NAN);
..\..\src\main.cpp(24): error:  #18: expected a ")"
        float foo(float a=NAN);
..\..\src\main.cpp(23): error:  #65: expected a ";"
        float f=NAN;
..\..\src\main.cpp: 0 warnings, 5 errors`

For me it looks like bug. I have never met same nuisance in GCC and other compilers.


Solution

  • 5 errors in 2 rows. Fantastic. WTF? MDK-ARM cannot resolve NAN.
    This could be fixed with next row:

    #define __ESCAPE__(__x)  (__x)
    

    Better make it for only this "cool" compiler:

    #ifdef __CC_ARM
     #define __ESCAPE__(__x)  (__x)
    #endif
    

    Another solution (not sure, but it could be compiler's extension):

    float f = 0f_7FC00000;