Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++26 freestanding feature-test macros #3837

Merged
merged 2 commits into from
Jul 14, 2023

Conversation

frederick-vs-ja
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja commented Jun 28, 2023

Fixes #3793. Fixes #3794. Fixes #3795.

(But we may need to reopen these issues and #2914 in future once freestanding mode is added. See #1289.)

It seems to me that hosted implementations are required to provide all of these macros (see "Rejected alternatives" in WG21-P2198R7). I think it makes more sense to define them in older modes when the corresponding feature/header is available.

Possibly related question: should we leave feature-test macros for non-core components not defined when _ENFORCE_ONLY_CORE_HEADERS is defined?

Apply them to older modes when the corresponding feature/header is available.
@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner June 28, 2023 18:02
@frederick-vs-ja
Copy link
Contributor Author

CC-ing @ben-craig.

@ben-craig
Copy link

(But we may need to reopen these issues and #2914 in future once freestanding mode is added. See #1289.)

You may want to drop a static assert somewhere checking __STDC_HOSTED__ so that these issues are noticed at that time.

It seems to me that hosted implementations are required to provide all of these macros (see "Rejected alternatives" in WG21-P2198R7). I think it makes more sense to define them in older modes when the corresponding feature/header is available.

The standard doesn't really talk about older modes, and I didn't do anything to audit which modes / standards would make sense. Sure, all the stuff in C++03 from the <utility> header is freestanding capable, but we've added stuff to <utility> since then. That could make backporting __cpp_lib_freestanding_utility weird.

@frederick-vs-ja
Copy link
Contributor Author

Sure, all the stuff in C++03 from the <utility> header is freestanding capable, but we've added stuff to <utility> since then. That could make backporting __cpp_lib_freestanding_utility weird.

The value 202306L may be misleading for some macros, but pehaps fine for others.

Macro Introduction of related portions Latest change of related portions at the time of 202306L Implemented in MSVC STL
__cpp_lib_freestanding_char_traits C++98 C++20 (char8_t, constexpr) Yes
__cpp_lib_freestanding_charconv C++17 C++26 (operator bool) No
__cpp_lib_freestanding_cstdlib C++98 C++23 (constexpr) No
__cpp_lib_freestanding_cstring C++98 (none?) Yes
__cpp_lib_freestanding_cwchar C++98 (none?) Yes
__cpp_lib_freestanding_errc C++11 (none?) Yes
__cpp_lib_freestanding_functional C++98 C++26 (function_ref etc.) No
__cpp_lib_freestanding_iterator C++98 C++23 (basic_const_iterator etc.) Yes
__cpp_lib_freestanding_memory C++98 C++23 (start_lifetime_as) No
__cpp_lib_freestanding_operator_new C++98 C++17 (aligned overloads) Yes (can be opted-out by /Zc:alignedNew-)
__cpp_lib_freestanding_ranges C++20 C++23 (various range adaptors) Yes
__cpp_lib_freestanding_ratio C++11 C++26 (optional additions) N/A (or Yes?)
__cpp_lib_freestanding_tuple C++11 C++23 (tuple-like) Yes
__cpp_lib_freestanding_utility C++98 C++23 (tuple-like) Yes

I don't actually know what __cpp_lib_freestanding_feature_test_macros intentionally covers.

@ben-craig
Copy link

ben-craig commented Jun 29, 2023

From the paper:

In order for users of freestanding implementations to be able to detect extensions of freestanding, the users need a way of distinguishing the C++20 macro requirements from an accurate expression of extension. This paper will provide a new macro, __cpp_lib_freestanding_feature_test_macros, so that users can distinguish between these cases. When __cpp_lib_freestanding_feature_test_macros is present, users will know that the C++20 feature-test macros aren't lying.

Example...

#if (__STDC_HOSTED__ || \
     (defined(__cpp_lib_freestanding_feature_test_macros) && \
      __cpp_lib_freestanding_feature_test_macros >= 202112))
#define TRUTHFUL_MACROS 1
#else
#define TRUTHFUL_MACROS 0
#endif

// see if filesystem happens to be supported on this possibly freestanding environment
#if TRUTHFUL_MACROS && defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L
// hosted and freestanding extension success path
#else
// fallback path
#endif

So that macro should only be important for actual freestanding implementations.

@StephanTLavavej StephanTLavavej added the cxx26 C++26 feature label Jun 30, 2023
@StephanTLavavej
Copy link
Member

Because these feature-test macros are essentially defined unconditionally, we can accept this as a special exception to our "no C++26 PRs yet" policy.

@StephanTLavavej StephanTLavavej self-assigned this Jul 4, 2023
@StephanTLavavej
Copy link
Member

Thanks! After fixing a copy-paste typo, looks perfect! 😻

@StephanTLavavej StephanTLavavej removed their assignment Jul 7, 2023
@StephanTLavavej StephanTLavavej self-assigned this Jul 13, 2023
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 068ce67 into microsoft:main Jul 14, 2023
35 checks passed
@StephanTLavavej
Copy link
Member

Thanks for implementing all of these feature-test macros! 🎉 ⚙️ 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cxx26 C++26 feature
Projects
None yet
4 participants