2

I have the following code in one of header files (lib.hpp file) of my project:

#ifndef SLIM_MATHS_LIB_HPP_
# define SLIM_MATHS_LIB_HPP_

namespace slim
{
namespace maths
{

namespace lib
{
template <typename T>
inline T        min(T a, T b); // Many errors on this line (see below)
// Other functions
}
}
}

# include "lib.ipp" // Functions definitions are inside

#endif // !SLIM_MATHS_LIB_HPP_

It compiled and worked very well with GCC on GNU/Linux system.

Now I am trying to compile it with Visual Studio 14.0 on Windows 10, and I got plenty of error on min function definition line, as following:

Error C2146 syntax error: missing ')' before identifier 'a' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2433 'T': 'inline' not permitted on data declarations slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2365 'T': redefinition; previous definition was 'template parameter' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2061 syntax error: identifier 'a' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2059 syntax error: ')' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2146 syntax error: missing ')' before identifier 'b' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2146 syntax error: missing ';' before identifier 'b' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23

I already successfully compiled it with Visual Studio 14.0 on a Windows 7 system, but lib.hpp and lib.ipp were respectively named lib.hh and lib.hpp, so I think it is a system problem from Windows 10 or an extention problem.
Maybe Visual Studio refuse to consider content of .ipp file as C++ code, as it doesn't color it as code when I open it. However, as it is included from an .hpp file and not directly added to solution, there shouldn't be a difference.

Aracthor
  • 5,757
  • 6
  • 31
  • 59
  • Just a thought - if you preprocess only with `cl /E`, what output do you see for the `inline T min(T a, T b);` line? I ask because if some header has a `#define min(a, b)`, it could be interfering with your version. – Tony Delroy Oct 28 '15 at 05:15
  • Similar question, though about attempted use of `std::min`, with a helpful answer mentioning `NOMINMAX` [here](http://stackoverflow.com/questions/5004858/stdmin-gives-error) – Tony Delroy Oct 28 '15 at 05:19

1 Answers1

2

min is defined as a macro in windows.h. Add a #undef min line to your header before declaring your function.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • 4
    This is why macros are evil. – Neil Kirk Oct 28 '15 at 05:17
  • 3
    @NeilKirk: even more - why Microsoft Visual C++ is evil: what an insane macro for a commonly used header to define.... – Tony Delroy Oct 28 '15 at 05:28
  • 1
    @TonyD It probably goes all the way back to the Windows 1.0 days and taking it out would break too much existing code, something MS seems to want to avoid. They could change it to opt-in (with a define) rather than opt-out with `NOMINMAX`. – 1201ProgramAlarm Oct 28 '15 at 05:29