<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3954: Feature-test macros in C headers (&lt;stddef.h&gt; etc.)</title>
<meta property="og:title" content="Issue 3954: Feature-test macros in C headers (&lt;stddef.h&gt; etc.)">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3954.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#New">New</a> status.</em></p>
<h3 id="3954"><a href="lwg-active.html#3954">3954</a>. Feature-test macros in C headers (<code>&lt;stddef.h&gt;</code> etc.)</h3>
<p><b>Section:</b> 17.15.1 <a href="https://wg21.link/support.c.headers.general">[support.c.headers.general]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2023-07-06 <b>Last modified:</b> 2024-02-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Several feature-test macros are available in C++ versions of C headers (e.g. <code>__cpp_lib_byte</code> is available in 
<code>&lt;cstddef&gt;</code>). However, the current standard wording doesn't seem to make the distinction between C++ library 
feature-test macros and macros from C, so it's not very clear that whether <code>&lt;stddef.h&gt;</code> and its friends are 
required to provide <code>__cpp_lib_*</code> macros in C++.
<p/>
Presumably, <code><i>name</i>.h</code> should provide macros shown in the synopsis of its corresponding <code>c<i>name</i></code> 
(as required in C), but should not be required to provide C++ library feature-test macros because such requirement would 
generally require implementations of the C standard library to change. I think we should make clarification in 
17.15.1 <a href="https://wg21.link/support.c.headers.general">[support.c.headers.general]</a>.
</p>

<p><i>[2024-02-22; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll in July 2023.
</p>
<p><i>[Jonathan commented]</i></p>

<p>
The issue says that "such requirement would generally require implementations
of the C standard library to change" but this is not true.
A conforming C++ library already needs a C++-aware <code>&lt;stdlib.h&gt;</code> and <code>&lt;math.h&gt;</code>, and has done so since C++98! We should be cautious about expanding the set of C headers that need to be C++-aware, but if we require <code>&lt;stdlib.h&gt;</code> to define these functions:
<pre><code>
constexpr long abs(long);
constexpr long long abs(long long);
</code></pre>
then it can also define <code>__cpp_lib_constexpr_math</code>.
</p>
<p>
We have these <code>&lt;cxxx&gt;</code> headers providing feature test macros:
</p>
<pre>
&lt;cmath&gt;:
__cpp_lib_constexpr_cmath
 __cpp_lib_hypot
__cpp_lib_interpolate
__cpp_lib_math_special_functions

&lt;cstddef&gt;:
__cpp_lib_byte

&lt;cstdlib&gt;:
__cpp_lib_constexpr_cmath
</pre>
<p>
For <code>&lt;stdlib.h&gt;</code> and <code>&lt;math.h&gt;</code>
the implementation already needs a C++-specific version of the header,
because <code>::abs</code> is required to be overloaded (and constexpr) in
<code>&lt;stdlib.h&gt;</code> and <code>&lt;math.h&gt;</code>
(and all the math functions have to be constexpr even if you include
<code>&lt;math.h&gt;</code>).
So I see no issue here: the <code>&lt;xxx.h&gt;</code> headers should obviously
define the same macros as the <code>&lt;cxxx&gt;</code> headers.
</p>
<p>
We do not require <code>::byte</code> to be in <code>&lt;stddef.h&gt;</code>,
so maybe we should not require the macro there either.
Except that <code>std::byte</code> is permitted to be in
<code>&lt;stddef.h&gt;</code>,
just not in the global namespace (see LWG <a href="lwg-active.html#3883" title="&sect;[support.c.headers.other] Ambiguity in the requirements for includes (Status: New)">3883</a><sup><a href="https://cplusplus.github.io/LWG/issue3883" title="Latest snapshot">(i)</a></sup>).
So maybe SD-6 should simply clarify that the macro indicates the presence
of <code>std::byte</code>, not <code>::byte</code>,
and whether <code>std::byte</code> and its macro are defined
by <code>&lt;stddef.h&gt;</code> is unspecified.
Also related to LWG <a href="lwg-active.html#3484" title="Should &lt;stddef.h&gt; declare ::nullptr_t? (Status: New)">3484</a><sup><a href="https://cplusplus.github.io/LWG/issue3484" title="Latest snapshot">(i)</a></sup>.
</p>

<p><i>[Jens commented]</i></p>


<p>
It seems 17.15 <a href="https://wg21.link/support.c.headers">[support.c.headers]</a> is silent on which macros
from the <code>&lt;cxxx&gt;</code> headers are made available via the <code>&lt;xxx.h&gt;</code> headers,
given that 17.15.7 <a href="https://wg21.link/support.c.headers.other">[support.c.headers.other]</a> talks about names placed in
the standard library namespace (macros don't fit that description).
</p>

<p><i>[Ben noted some additional macros]</i></p>

<pre><code>
#define __cpp_lib_freestanding_cstdlib      <em>new-val // freestanding, also in &lt;cstdlib&gt;, &lt;cmath&gt;</em>
#define __cpp_lib_freestanding_cstring      <em>new-val // freestanding, also in &lt;cstring&gt;</em>
#define __cpp_lib_freestanding_cwchar       <em>new-val // freestanding, also in &lt;cwchar&gt;</em>
#define __cpp_lib_freestanding_errc         <em>new-val // freestanding, also in &lt;cerrno&gt;, &lt;system_error&gt;</em>
</code></pre>



<p id="res-3954"><b>Proposed resolution:</b></p>





</body>
</html>
