7

C11 introduced <stdalign.h>, which defined the macros alignas to _Alignas and alignof to _Alignof. Additionally, the feature test macros __alignas_is_defined and __alignof_is_defined are defined to 1.

C23 introduces alignas and alignof as keywords, and defines that <stdalign.h> "defines no content".

C++23 (quite likely unaware of the change in C23) states that <stdalign.h> is "the same as the C standard library include", with the one difference that it "does not define a macro named alignas".

Do I understand this correctly that, in C++, <stdalign.h> does define alignof as a macro, and defines __alignas_is_defined / __alignof_is_defined to 1, whereas in C23 it does none of these things anymore?

8
  • Maybe look up <cstdalign>. en.cppreference.com/w/cpp/header/cstdalign.html Commented Oct 9 at 20:59
  • 5
    @DevSolar The C++23 Standard is based on the C18 Standard: "2 C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:2018 Programming languages" Commented Oct 9 at 21:01
  • 2
    C++11 had keywords alignas and alignof. C was cautious about introducing new names as keywords because of the danger of breaking existing code innocently using the keyword as an identifier. The names _Alignof and _Alignas are in the namespace reserved for the "implementation", so anyone using those names before they became keywords had only themselves to blame. The header allowed you to use the lower-case names (alignof and alignas), but didn't break existing compliant code. […continued…] Commented Oct 9 at 21:31
  • 1
    […continuation…] C23 managed to get over the aversion to new keywords, and bool, true, false, static_assert and thread_local are now keywords, too, as well as alignof and alignas. There are a number of other new keywords. The underscore-capital names are documented as being obsolescent, but continue to be supported for backwards compatibility. Commented Oct 9 at 21:34
  • @JonathanLeffler That much is obvious. What's stumping me is the C++ standard mentioning only half of what's in stdalign.h -- and how existing implementations do yet separate things with that header. I am trying to figure out the "most correct" way to implement the header with regards to C11 / C17 / C23 / C++11 / C++17 / C++23, as the wording of the standards somehow feels... incomplete. Commented Oct 10 at 4:34

1 Answer 1

5

C++23 and C23 are not "harmonized", meaning no effort has been made to make them compatible. If you peek into the C++23 standard chapter 2 "normative references", you'll find:

(1.3) — ISO/IEC 9899:2018, Programming languages — C

So when C++23 speaks of the C standard, it speaks of C17. Which has the same implementation of stdalign.h and similar headers as C11 had.

As for C23 the header is indeed empty and all the headers that were included for C++ compatibility in C11 are getting phased out. C23 added the various features as proper keywords like in C++: alignas, alignof, bool, true, false, static_assert, thread_local. For now it also allows the alternative spellings _Alignas and so on, that were added as keywords in C11 (see C23 6.4.2 table 6.1).

Sign up to request clarification or add additional context in comments.

1 Comment

This :) It keeps confusing me that people still think "C" and "C++" are the same language. "C++" is kept (largely) compatible with "C" (so it can call "C" functions). But they are indeed two languages that should be considered completely independent with their own rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.