Guard [[unlikely]] attribute use with C++20 language check (fixes #427)

Clang recognizes `__has_cpp_attribute(unlikely)` even when compiling in
C++17 mode (`-std=c++17`), but using `[[unlikely]]` before C++20 emits
`-Wc++20-attribute-extensions` under `-pedantic`.

Require C++20 or later (`__cplusplus >= 202002L` or `_MSVC_LANG >= 202002L`)
before enabling `FASTFLOAT_USE_UNLIKELY_ATTR`. Pre-C++20 builds on GCC
and Clang will continue to use `__builtin_expect(!!(x), 0)` without extension
warnings.

Fixes #427
Co-authored-by: ByteAsk <support@byteask.ai>
This commit is contained in:
Jisan Halder 2026-10-18 19:44:06 +05:30
parent 0de7a21da5
commit defb5e48f2

View File

@ -257,6 +257,7 @@ using parse_options = parse_options_t<char>;
// (the macro supplies the parentheses). It expands to the standard [[unlikely]]
// attribute when supported, otherwise to __builtin_expect on GCC/Clang, or
// to a no-op elsewhere (e.g. pre-C++20 MSVC, which has no equivalent hint).
#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#ifdef __has_cpp_attribute
#if __has_cpp_attribute(unlikely) >= 201803L
// g++-9 hits hits this branch, but then fails to compile
@ -266,6 +267,7 @@ using parse_options = parse_options_t<char>;
#endif
#endif
#endif
#endif
#ifdef FASTFLOAT_USE_UNLIKELY_ATTR
#define fastfloat_unlikely(x) (x) [[unlikely]]