chansen / p5-time-moment

Time::Moment represents an exact moment in time.
32 stars 8 forks source link

Redefinition of bool with Visual Studio 2013 C compiler #12

Closed nanis closed 9 years ago

nanis commented 9 years ago

src/dt_config.h has this:

#if !defined(_MSC_VER)
# include <stdbool.h> 
#endif

#if !defined(__cplusplus) && !defined(__bool_true_false_are_defined)
typedef char _Bool;
# define bool _Bool
# define true 1
# define false 0
# define __bool_true_false_are_defined 1
#endif

which causes:

...\time-moment-0.21\src\dt_config.h(34) : error C2632: 'char' followed by 'bool' is illegal

...\time-moment-0.21\src\dt_config.h(34) : warning C4091: 'typedef ' : ignored on left of 'char' when no variable is declared

Removing the #ifdef guard around #include <stdbool.h> solves the problem, but, of course, I do not know when it became OK to use stdbool.h on this platform. I have

cl /?
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64

and `_MSC_VER` is `1800`.
nanis commented 9 years ago

Note that Visual Studio 2013 documentation has this to say:

In Visual C++4.2, the Standard C++ header files contained a typedef that equated bool with int. In Visual C++ 5.0 and later, bool is implemented as a built-in type with a size of 1 byte. That means that for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1. This can cause memory corruption problems if you have defined structure members of type bool in Visual C++ 4.2 and are mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later compilers.

The __BOOL_DEFINED macro can be used to wrap code that is dependent on whether or not bool is supported.

So, I am not sure if the unconditional definitions are necessary even on earlier platforms, but stdbool.h was added in Visual Studio 2013:

... We also added the new headers complex.h, stdbool.h, fenv.h, and inttypes.h, and added the implementations for all the functions declared in them.

Therefore, stdbool.h can, and needs to be, safely included if _MSC_VER >= 1800.

chansen commented 9 years ago

Thank you for the report! I have released 0.22 on CPAN which should fix this issue.

nanis commented 9 years ago

Thank you very much for the heads up.