Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializer is not a constant only with certain functions

If I try compile the following and I have /O2 optimisation on, then it doesn't compile:

#include <math.h>


double myfunc(double arg) { return 5.4; };


double (*f[])(double) = { 
    myfunc,
    fabs, 
   // floor,  // THESE TWO TRIGGER THE ERROR
   // ceil, // initialiser is not a constant 
    sqrt
};

Only the floor and ceil functions trigger the error. If I put this code in a cpp file it compiles fine, but it seems MSVC's compiler is bugged, or am I doing something wrong? Error only happens on /O2, not /O1 or disabled. I'm on Visual Studio Version 17.7.6. Windows SDK version is 10.0 (latest installed version).

like image 802
Zebrafish Avatar asked Oct 25 '25 07:10

Zebrafish


1 Answers

It is unrelated to the optimisation settings.

It is only because of the Enable Intrinsic Functions settings. If you set this to NO, it compiles fine. With this setting set to Yes (/Oi) I get the same errors.

  • Visual Studio 2022 17.8.0
  • Platform toolset : Visual Studio 2022 (v143)
  • Windows SDK Version : 10.0.22621.0 (latest installed version)

enter image description here

like image 116
Jabberwocky Avatar answered Oct 26 '25 21:10

Jabberwocky



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!