codeplea/tinyexpr

"initializer is not a constant" error in Visual Studio.

cuinjune opened this issue · 7 comments

Hi, I tried to build tinyexpr using Visual Studio on Windows 10 but an error occurs saying "initializer is not a constant".
screen shot 2018-01-21 at 4 04 27 am
If I remove all elements in functions[] array, the error no longer occurred.
Could someone please teach me how to fix this error?
Thank you so much in advance!

I don't have VS installed right now, so this is only a wild guess, but what would happen if you replace TE_FUNCTION1 | TE_FLAG_PURE with only TE_FUNCTION1? Please try that and let me know.

It would be pretty sad if windows C compiler cannot solve that into a constant expression... It could also be that we are not feeding a size to that array. If all else fails, we might have to have ifdefs specific to that platform, and possibly add a constructor attribute to an initialization function... in gcc this would be easy, although I'm not aware of how it would work on Visual's C compiler.

@codeplea Hi, I actually modified the original code a bit.
if you see my screenshot, I added 'f's to the end of the functions. (e.g acos -> acosf)
Removing all these 'f's almost fixed the problem.
Now only two functions generate this error that are "ceil" and "floor".
These functions generate the error even after removing 'f's from the function.
Any idea why this happens and how can I use "ceil" and "floor" without the error?
Thank you so much.

@codeplea I tried changing "ceil" and "floor" to "ceill" and "floorl" and the error no longer occurred.
This is pretty strange.

I could totally fix this error by creating separate static functions like how fac(), ncr() and npr() are currently implemented.

For example for ceil(), I created this function.
static double func_ceil(double a) {return ceil(a);}

And then call it when initializing the te_variable functions[]array.
{"ceil", func_ceil, TE_FUNCTION1 | TE_FLAG_PURE, 0},

I did this to all the elements of te_variable functions[]array.
This way, I could avoid the weird "initializer is not a constant" error in Visual Studio.
Maybe you can apply this change to your code? (if you think it's necessary)
Thanks anyway!

@cuinjune What version of Visual Studio are you using?

Here is a discussion where someone else is having the same problem almost 10 years ago. There are a few workaround ideas there too.

@codeplea Hey I just tried your link and I could fix the issue by simply adding the below two lines anywhere above static const te_variable functions[] = {}

#pragma function (ceil)
#pragma function (floor)

Now it works without the problem and it is a lot simpler than my above solution.
Thank you so much for your finding!

You can close this issue if you want.