stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.09k stars 111 forks source link

Build failure when including Windows.h header #652

Closed Yimura closed 8 months ago

Yimura commented 8 months ago

Env

Compiler Output

Compiler Output ```log [build] C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1438~1.331\bin\Hostx64\x64\cl.exe /nologo /TP -external:IC:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include -external:W0 /DWIN32 /D_WINDOWS /EHsc /Zi /O2 /Ob1 /DNDEBUG -std:c++latest -MD /Zc:preprocessor /permissive- /Zc:lambda /showIncludes /FoCMakeFiles\min_reproducible.dir\main.cpp.obj /FdCMakeFiles\min_reproducible.dir\ /FS -c C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\main.cpp [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(17): error C2589: '(': illegal token on right side of '::' [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(17): error C2062: type 'unknown-type' unexpected [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(17): error C2059: syntax error: ')' [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(15): error C3615: constexpr function 'glz::detail::is_safe_addition' cannot result in a constant expression [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(15): note: failure was caused by control reaching the end of a constexpr function [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(22): error C2589: '(': illegal token on right side of '::' [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(22): error C2062: type 'unknown-type' unexpected [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(22): error C2059: syntax error: ')' [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/util/stoui64.hpp(23): error C3536: 'b': cannot be used before it is initialized [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/csv/read.hpp(70): error C2589: '(': illegal token on right side of '::' [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/csv/read.hpp(50): note: This diagnostic occurred in the compiler generated function 'void glz::detail::from_csv::op(_T0 &&,_T1 &&,It &&,_T2 &&) noexcept' [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/csv/read.hpp(70): note: the template instantiation context (the oldest one first) is [build] C:\Users\localadmin\Documents\Coding\cpp\MinReproducable\build\_deps\glaze-src\include\glaze/csv/read.hpp(104): note: see reference to class template instantiation 'glz::detail::from_csv' being compiled ... ```

Minimal Reproducible Example

CMakeLists.txt

cmake_minimum_required(VERSION 3.20.x)

project(min_reproducible)

add_executable(${PROJECT_NAME} main.cpp)

set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 23)

include(FetchContent)
FetchContent_Declare(glaze GIT_REPOSITORY https://github.com/stephenberry/glaze.git GIT_TAG v1.9.8.1 GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(glaze)

target_link_libraries(${PROJECT_NAME} PRIVATE glaze::glaze)

main.cpp

#include <Windows.h>
#include <glaze/glaze.hpp>

int main()
{

    return 0;
}

Solutions

I found the following related issue #169 on this repo that mentioned a similar thing and was able to find out that the same bug had occurred.

Either add the following right after FetchContent including glaze into your repository (solution for people including glaze):

add_compile_definitions(glaze_glaze NOMINMAX)

Or as #169 stated guard the functions in parentheses and it will dissappear:

diff --git a/include/glaze/csv/read.hpp b/include/glaze/csv/read.hpp
index 0565a01..5e6216a 100644
--- a/include/glaze/csv/read.hpp
+++ b/include/glaze/csv/read.hpp
@@ -67,7 +67,7 @@ namespace glz
                      return;
                   }

-                  if (i > std::numeric_limits<V>::max()) [[unlikely]] {
+                  if (i > (std::numeric_limits<V>::max)()) [[unlikely]] {
                      ctx.error = error_code::parse_number_failure;
                      return;
                   }
@@ -86,7 +86,7 @@ namespace glz
                      return;
                   }

-                  if (i > std::numeric_limits<V>::max()) [[unlikely]] {
+                  if (i > (std::numeric_limits<V>::max)()) [[unlikely]] {
                      ctx.error = error_code::parse_number_failure;
                      return;
                   }
diff --git a/include/glaze/util/stoui64.hpp b/include/glaze/util/stoui64.hpp
index 7fce095..c7309a6 100644
--- a/include/glaze/util/stoui64.hpp
+++ b/include/glaze/util/stoui64.hpp
@@ -14,12 +14,12 @@ namespace glz::detail

    GLZ_ALWAYS_INLINE constexpr bool is_safe_addition(uint64_t a, uint64_t b) noexcept
    {
-      return a <= std::numeric_limits<uint64_t>::max() - b;
+      return a <= (std::numeric_limits<uint64_t>::max)() - b;
    }

    GLZ_ALWAYS_INLINE constexpr bool is_safe_multiplication10(uint64_t a) noexcept
    {
-      constexpr auto b = std::numeric_limits<uint64_t>::max() / 10;
+      constexpr auto b = (std::numeric_limits<uint64_t>::max)() / 10;
       return a <= b;
    }
stephenberry commented 8 months ago

Thanks for pointing this out. Fixed with #654