Why does the C++ standard not mention __STDC_IEC_559__? - c++

According to C++11 standard [c.math], the <cmath> header is same as Standard C library header <math.h>.
(Of course, there are several differences, --- namespace, overloads etc. --- but these can be ignored here.)
And according to C99 standard annex F, "An implementation that defines __STDC_IEC_559__ shall conform to the specifications in" the annex F.
Ex. The atan2 may cause a domain error if both arguments are zero, but It must not if __STDC_IEC_559__ is defined.
In C99, many behavior is also dependent on whether __STDC_IEC_559__ is defined or not.
However, it seems that __STDC_IEC_559__ is not mentioned anywhere in C++11 standard.
If so, shall a C++ implementation conform to the specifications in the annex F?
I think that std::numeric_limits<T>::is_iec559() is a substitute, but it seems to mention about only type.

The C++ standard (n3797) includes the C standard library by reference, see s1.2/2.
The library described in Clause 7 of ISO/IEC 9899:1999 and Clause 7 of ISO/IEC 9899:1999/Cor.1:2001
and Clause 7 of ISO/IEC 9899:1999/Cor.2:2003 is hereinafter called the C standard library.
With the qualifications noted in Clauses 18 through 30 and in C.4, the C standard library is a subset of the C++ standard
library.
The standard contains no mention of that symbol, and I would not expect it be defined, since it appears to be specific to Standard C. By not defining that symbol, C++ is not bound by the contents of Annex F.
Instead the C++ standard contains multiple mentions of IEC 559 in a rather more C++-like form. For example,
Shall be true for all specializations in which is_iec559 != false
There is a specific mention in 18.3.2.4/56.
static constexpr bool is_iec559;
True if and only if the type adheres to IEC 559 standard.218
Meaningful for all floating point types.
I think it would be fair to say that C++ includes all the same capabilities (or lack of them), but adapted to the C++ world.

Related

Is memcpy(dest, src, 0) defined in the C++ standard?

This question and many more are among the numerous questions on this site that ask if a call to memcpy() is valid with a length\size specified with a zero value.
When answering, everyone quotes the current C standard (in this case C17 ISO/IEC9899:2017 page 283 ),
Where an argument declared as size_t n specifies the length of the array for a function, n can have
the value zero on a call to that function. Unless explicitly stated otherwise in the description of a
particular function in this subclause, pointer arguments on such a call shall still have valid values, as
described in 7.1.4. On such a call, a function that locates a character finds no occurrence, a function
that compares two character sequences returns zero, and a function that copies characters copies
zero characters.
However, this is from C standard, not quoted from the C++ standard.
Where in the current C++ standard (i.e. C++17 ISO/IEC 14882) is this same definition listed? C and C++ have two different standards (and languages) and from my understanding, you cannot quote one and expect that same rule/behavior to be present in the other standard.
If this quotation from the C standard is valid in C++ without explicitly stating it in the standard, can someone then provide a source that backs up this connection between C and C++?
The C++17 standards says this about the C standard library:
1
The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.
2
The descriptions of many library functions rely on the C standard library for the semantics of those functions. In some cases, the signatures specified in this International Standard may be different from the signatures in the C standard library, and additional overloads may be declared in this International Standard, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.
As to your question,
If this quotation from the C standard is valid in C++ without explicitly stating it in the standard
The answer is "yes".
[library.c]/2, emphasis mine:
The descriptions of many library functions rely on the C standard library for the semantics of those functions.
In some cases, the signatures specified in this document may be different from the signatures in the C standard library, and additional overloads may be declared in this document, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.
[cstring.syn]/1:
The contents and meaning of the header <cstring> are the same as the C standard library header <string.h>.
The C library is sort of baked into C++. As said in [intro.scope] from the C++17 standard (emphasis mine):
C++ is a general purpose programming language based on the C programming language as described in
ISO/IEC 9899:2011 Programming languages — C (hereinafter referred to as the C standard). In addition to
the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces,
operator overloading, function name overloading, references, free store management operators, and additional
library facilities.
And again, in [intro.refs]:
The library described in Clause 7 of ISO/IEC 9899:2011 is hereinafter called the C standard library.1
1) With the qualifications noted in Clauses 21 through 33 and in C.5, the C standard library is a subset of the C++ standard
library.
So everything that is in the C standard library is in C++.

Relationship between C and C++ standard library

This question doesn't directly relate to programming or a specific language concept. My question is can we use a reference to the C International Standard (for instance C11) to provide a normative reference to describe any concept from a C library in C++.
To be more specific, in a header <climits> defined in N3797::18.3.3 [c.limits] the C library header is described. But the C Standard provide more comprehensive information about <limits.h>'s content rather than N3797 working draft.
So everything about the C library defined in the C11 is true for the C++ implementation defined in C++11 of C library or we can't rely to what the C standard provides?
For the C standard library C++ falls back on the C standard and for C++11 it falls back on C99 not C11, before C++11 the C standard referenced was C90. This is covered in section 1.2 Normative references which says:
The following referenced documents are indispensable for the
application of this document. For dated references, only the edition
cited applies. For undated references, the latest edition of the
referenced document (including any amendments) applies.
and includes:
ISO/IEC 9899:1999, Programming languages — C
ISO/IEC 9899:1999/Cor.1:2001(E), Programming languages — C, Technical Corrigendum 1
ISO/IEC 9899:1999/Cor.2:2004(E), Programming languages — C, Technical Corrigendum 2
ISO/IEC 9899:1999/Cor.3:2007(E), Programming languages — C, Technical Corrigendum 3
and also says:
The library described in Clause 7 of ISO/IEC 9899:1999 and Clause 7 of
ISO/IEC 9899:1999/Cor.1:2001 and Clause 7 of ISO/IEC
9899:1999/Cor.2:2003 is hereinafter called the C standard library.1
The C++ standard uses the term C standard library to refer back to C99 and the TCs and will explicitly state when C++ differs from C.
and section 17.2 The C standard library says:
The C++ standard library also makes available the facilities of the
C standard library, suitably adjusted to ensure static type safety.
The descriptions of many library functions rely on the C standard
library for the signatures and semantics of those functions. In all
such cases, any use of the restrict qualifier shall be omitted.
The cname header files which correspond to C Standard Library name.h files is covered in 17.6.1.2 Headers which says amongst other things:
Except as noted in Clauses 18 through 30 and Annex D, the contents of
each header cname shall be the same as that of the corresponding
header name.h, as specified in the C standard library (1.2) or the C
Unicode TR, as appropriate, as if by inclusion. In the C++ standard
library, however, the declarations (except for names which are defined
as macros in C) are within namespace scope (3.3.6) of the namespace
std. It is unspecified whether these names are first declared within
the global namespace scope and are then injected into namespace std by
explicit using-declarations (7.3.3)
The contents of climits as they relate to limits.h is covered in section 18.3.3 and says:
The contents are the same as the Standard C library header .
[ Note: The types of the constants defined by macros in are
not required to match the types to which the macros refer.—end note ]
Note, as I mentioned in the comment above, the normative references are not taken as a whole, the C++ standard must make explicit reference to a normative reference for it to apply to the C++ standard. See Can we apply content not explicitly cited from the normative references to the C++ standard? for more details.

putc implemented as Macro ic C++?

I know Macro implementation of putc() in C, but is it same in C++?
It will depend on your implementation of cstdio. In most cases this is really just a wrapper around stdio.h, with wrappers declared inside the std namespace, and the C and C++ compilers share the same standard library for C functions. For example, VS2010 uses stdio.h for C++, in which putc is implemented as both a macro and a function, depending on environment and other compile-time definitions.
Which version of C++? C++83 (1983)? C++98 (1998)? C++11 (2011)?
The C++98 and C++11 Specifications rely on the ISO C specifications for C Library functions, and do not put additional implementation constraints on them, other than trivial ones like renaming stdio.h to cstdio.h and allowing inclusion without the dot-h suffix.
See: C++98 Specification
See: C++11 Specification
Look in cstdio.h if you are interested in your particular compiler.
However, if we dig deeper and take a look at the ISO C standard: "ISO/IEC 9899:1990" (C89/C90), well, we find that it is unavailable for free viewing on the web (not even the final draft standard), so moving on to C99 (NOT ISO C), you find...
...that C99 (Not "ISO C") says putc() MAY be implemented as a macro,
See: C99 Specification
So if you are really developing in Obj-C++ (which uses C99), then C99 is the relevant specification to consider, not ISO C (C90). Also, since C99 lets the compiler writer decide whether to make putc() a macro or not, you should consider it an open possibility, and decide whether you really care to know about the C90 (ISO C) spec which is becoming obsolete (now that even C11 (2011) is out.)
Yes it is. Both C and C++ use <stdio.h> which has the same scheme in all implementations that I know of.

Is a C++ preprocessor identical to a C preprocessor?

I am wondering how different the preprocessors for C++ and C are.
The reason for the question is this question on a preprocessor-specific question where the paragraph of the standard that addresses the question has a different wording (and a different paragraph number) and also are difference concerning the true and false keywords in C++.
So, are there more differences or is this the only difference.
An extension of the question would be when is a source file emitted differently by a C++ preprocessor and a C preprocessor.
The C++03 preprocessor is (at least intended to be) similar to the C preprocessor before C99. Although the wording and paragraph numbers are slightly different, the only technical differences I'm aware of between the two are that the C++ preprocessor handles digraphs (two-letter alternative tokens) and universal character names, which are not present in C.
As of C99, the C preprocessor added some new capabilities (e.g., variadic macros) that do not exist in the current version of C++. I don't remember for sure, but don't believe that digraphs were added.
I believe C++0x will bring the two in line again (at least that's the intent). Again, the paragraph numbers and wording won't be identical, but I believe the intent is that they should work the same (other than retaining the differences mentioned above).
They are supposed to be the same: C++98 and C++03 should match C90, and C++0x should match C99. There may be bugs in the wording, though.
Predefined macros differ between the preprocessors, mostly for obvious language feature differences. E.g. compare:
C99 N1256 draft 6.10.8 "Predefined macro names"
C++11 N3337 draft 16.8 "Predefined macro names"
In particular:
C requires you not to define __cplusplus, C++ uses it to represent the version
C uses __STDC__ to represent the version, C++ says is implementation defined and uses __cplusplus instead
C has __STDC_IEC_559__ and __STDC_IEC_559_COMPLEX__ to indicate floating point characteristics, C++ does not and seems replace that with the per type std::numeric_limits<float>::is_iec559 constants
C does not have the macros prefixed with __STDCPP: _STDCPP_STRICT_POINTER_SAFETY__ and __STDCPP_THREADS__
As mentioned by DevSolar, C11 added many more defines which are not part of C++11.

Is the C part of the C++ library automatically C99?

Are all the functions in a conformant C++98/03/0x implementation completely C99 conformant?
I thought C++0x added some C99 (language) features, but never heard or read anything definitive about the C library functions.
Just to avoid any confusion, I'm talking about a C++ program using functions declared in the <c*> header set.
Thanks.
Most of the C99 standard library has been imported in C++0X but not all. From memory, in what wasn't imported there are
<ctgmath> simply includes <ccomplex> and <cmath>,
<ccomplex> behaves as if it included <complex>
<cmath> has quite a few adjustment (providing overload and template functions completing the C99 provided one)
Some other headers (<cstdbool>, <iso646.h>, ...) have adjustments to take differences between language into account (bool is primitive in C++, a macro provided by <stdbool.h> in C for instance), but nothing of the scope of the math part.
The headers <xxx.h> whose <cxx> form doesn't behaves as the C99 version simply declares the content of <cxxx> in the global namespace, they aren't nearer of the C99 <xxx.h> content.
A related thing: C++0X provides some headers in both cxxx and xxx.h forms which aren't defined in C99 (<cstdalign> and <cuchar>, the second one is defined in a C TR)
(I remembered that a bunch of mathematical functions from C99 had been put in TR1 but not kept in C++0X, I was mistaken, that bunch of mathematical functions weren't part of C99 in the first place).
No. C++03 is aligned with ANSI C89/ISO C90, not C99.
The upcoming C++0x standard is expected to be aligned to some degree with C99. See paragraph 17.6.1.2 in the current draft which lists ccomplex, cinttypes, cstdint etc. Note that, as AProgrammer mentions, some headers aren't exactly the same; further, that the header cuchar is aligned with the C Technical Report 19769 rather than C99.