Skip to content

Fall back to -std=c++2a on compilers without -std=c++20 - #105

Open
bernardladenthin wants to merge 1 commit into
sheredom:mainfrom
bernardladenthin:fix/test-cxx20-flag-fallback
Open

Fall back to -std=c++2a on compilers without -std=c++20#105
bernardladenthin wants to merge 1 commit into
sheredom:mainfrom
bernardladenthin:fix/test-cxx20-flag-fallback

Conversation

@bernardladenthin

Copy link
Copy Markdown

Problem

test/CMakeLists.txt compiles test20.cpp with a hardcoded -std=c++20. GCC 8 — still
the system compiler on RHEL 8 and its rebuilds — only implements C++20 under the
pre-release spelling and rejects the flag outright:

g++-8: error: unrecognized command line option '-std=c++20'; did you mean '-std=c++2a'?

The test suite therefore cannot be built with it at all, although the library itself
compiles fine.

Fix

Probe the flag instead of the CMake feature. CMAKE_CXX_COMPILE_FEATURES lists
cxx_std_20 on GCC 8, because CMake maps that feature onto -std=c++2a — so the feature
list cannot tell us which spelling the compiler accepts. check_cxx_compiler_flag can,
and -std=c++2a is used where -std=c++20 is unknown.

This keeps test20.cpp a real C++20 translation unit on those compilers, rather than
silently falling back to the default standard.

Testing

Ubuntu 20.04 (glibc 2.31, so the library builds there regardless of #104), with g++-8
8.4.0:

Result
before build fails at test20.cpp: unrecognized command line option '-std=c++20'
after 432 pass

Regression checks, all unchanged:

Compiler Environment Result
GCC 13.3 Ubuntu 24.04 432 pass
clang 18.1 Ubuntu 24.04 432 pass
MSVC 19.44 Windows, Ninja 387 pass

On MSVC the probe correctly reports failure — cl.exe only warns (D9002: ignoring unknown option), but CMake's built-in fail patterns catch that. The -std=c++2a
fallback is then discarded anyway, because subprocess_set_cxx_flags skips the standard
flag whenever SUBPROCESS_CXX_USE_STD_FLAG is false.

GCC 8 implements C++20 under the pre-release spelling only. CMake still lists
cxx_std_20 as supported there because it maps the feature onto -std=c++2a, so
the feature list cannot tell us which spelling the compiler accepts. Probe the
flag itself instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant