In a project of mine, I have a header file with the following code:
#if __cplusplus >= 201703L
#include <optional>
#include <any>
namespace foo {
using ::std::optional;
using ::std::nullopt;
} // namespace foo
#elif __cplusplus >= 201402L
#include <experimental/optional>
#include <experimental/any>
namespace foo {
using ::std::experimental::optional;
using ::std::experimental::nullopt;
} // namespace foo
#else
// define an optional class myself
// etc. etc.
#endif
This file compiles fine with GCC on Linux. On Windows, however, this happens (lines broken for readability:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\CL.exe
/c /I"D:\a\libfoo\libfoo\src"
/nologo /W4 /WX- /diagnostics:column /O2 /Ob2 /D _MBCS /D WIN32 /D _WINDOWS /D NDEBUG
/D _CRT_SECURE_NO_DEPRECATE /D _CRT_SECURE_NO_WARNINGS
/D _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING /D "CMAKE_INTDIR=\"Release\""
/Gm- /EHsc /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"prog.dir\Release\\"
/Fd"prog.dir\Release\vc142.pdb" /external:W0 /Gd /TP /errorReport:queue
/Zc:__cplusplus "D:\a\libfoo\libfoo\prog\prog.cpp"
2023-04-17T19:12:53.5199300Z prog.cpp
2023-04-17T19:12:53.6223921Z D:\a\libfoo\libfoo\src\foo\detail/optional.hpp(20,10):
fatal error C1083: Cannot open include file: 'experimental/optional': No such file or directory
Two things are happening here:
- The C++14 code is being executed, and
- MSVC fails to locate
<experimental/optional>
Now, given (1.), I don't understand how (2.) can happen. If MSVC is in C++14 mode, the experimental headers for C++14 should be available. So what's going on? And how can I get MSVC to successfully compile my program ?
Notes:
- This is a GitHub action runner VM, so I assume it's not a machine configuration problem.
- Non-experimental C++ headers are found by the compiler