KhronosGroup / Vulkan-Hpp

Open-Source Vulkan C++ API
Apache License 2.0
3.13k stars 307 forks source link

std::min clashes with macros in windows.h #1893

Closed YaaZ closed 4 months ago

YaaZ commented 4 months ago

I started getting the following compilation errors on MSVC:

vulkan/vulkan.hpp(152): error C2589: '(': illegal token on right side of '::'
vulkan/vulkan.hpp(152): note: the template instantiation context (the oldest one first) is
vulkan/vulkan.hpp(159): note: see reference to class template instantiation 'vk::ArrayWrapper1D<T,N>' being compiled
vulkan/vulkan.hpp(152): error C2760: syntax error: ')' was unexpected here; expected ';'
vulkan/vulkan.hpp(152): error C3878: syntax error: unexpected token ')' following 'expression_statement'
vulkan/vulkan.hpp(152): note: error recovery skipped: ')'

The token it complains about is min:

size_t n = std::min( N - 1, len );

Workaround: define NOMINMAX before including windows.h in vulkan.h.

asuessenbach commented 4 months ago

You're right, defining NOMINMAX before including windows.h is the right thing to do. Unfortunately, windows.h is a bit too aggressive here and bites every program that's using std::min or std::max.

YaaZ commented 4 months ago

But that's a thing we cannot always control. What if one manually includes vulkan.h without NOMINMAX before vulkan.hpp? Or even worse, manually including windows.h?

YaaZ commented 4 months ago

Idk, maybe we could do something like this?

#push_macro("min")
#undef min
...
#pop_macro("min")
SaschaWillems commented 4 months ago

Was this a recent change? I'm seeing a similar build failure in our Vulkan Samples repo, which stops me from updating to a recent header version.

asuessenbach commented 4 months ago

Yes, it seems there slipped some std::min or std::max into one of the generated headers lately... Will be fixed with #1906.