Appendix C: ISO C++ 2003 Compatibility, Revision 7
ISO/IEC JTC1 SC22 WG21 N3288=11-0058 2011-03-25
Benjamin Kosnik, bkoz@redhat.com
Addresses: UK 6
Introduction
This paper details compatibility of C++ 2003 code, as compiled by this latest version of the C++ standard. It is based on ISO C++ draft N3242, and is a successor to N3186, Appendix C: ISO C++ 2003 Compatibility. This is the seventh revision of the compatibility document.
Changes for each version are as follows.
v2:
remove 27.5.2
27.5.5.4 basic_ios use of explicit operator bool
27.7.2.1.3 basic_istream sentry use of explicit operator bool
27.7.3.4 basic_ostream sentry use of explicit operator bool
20.6.4 garbage collection
20.2.2 swap moved headers
17.6.4.3.1 keywords as macros are now verboten
15.4 throw() to noexcept and lack of unexpected
identifiers
23.2 container requirements that were relaxed
23.2.3/23.2.4 signature changes reference to value
23.2.3/23.2.4 signature changes iterator to const_iterator changes
26.4 complex C99 compatibility
v3:
26.8 cmath new identifiers
12.4 clarify destructor throwing vs. new implicit noexcept
18.6.1.1 wording improved
14.1 wording change
added alignas as a keyword, removed static_cast
8.5.4 narrowing restrictions in aggregate initialization
2.5 raw string note
14.6.4.2 overload resolution relaxation allowing static functions
5.6 rounding towards zero now explicitly specified
2.14.2 long long constants changed from unsigned to signed
v4:
remove specifying identifiers, point at the index of library names
12.1, 12.4, 12.8 implicitly-deleted special member functions
21.4.1 string invalidation changes
20.8.4/8 function objects no longer derived
remove 27.3 sync
18.x add operator new vs. bad_alloc
23.2 default constructible
clarify wording
v5:
2.5 user-defined literals vs. preprocessing tokens
wording changes
20.3.3 make_pair
v6:
wording changes
remove make_pair
v7:
wording changes
Proposed wording
Add a new section to Appendix C with the following contents.
C.2 C++ and ISO C++2003 [diff.iso.ISO/IEC 14882:2003]
The following subclauses list the differences between this International Standard and the previous version of C++ (ISO/IEC 14882:2003 Programming Languages - C++), by the chapters of this document.
C.2.1 Clause 2 lexical conventions
2.5
Change: New kinds of string literals
Rationale: Required for new features.
Effect on original feature: Valid ISO/IEC 14882:2003 code may fail to
compile or produce different results in this International Standard.
Specifically, macros named R, u8, u8R, u, uR, U, UR, or LR will not
be expanded when adjacent to a string literal but will be interpreted
as part of the string literal. For example,
#define u8 "abc"
const char* s = u8"def"; // Previously "abcdef" now "def"
2.5
Change: User-defined literal string support
Rationale: Required for new features.
Effect on original feature: Valid C Standard or ISO/IEC 14882:2003 code may fail to compile in this International Standard, as the following example illustrates.
#define _x "there"
"hello"_x // Line 1
Previously, line 1 would have consisted of two separate preprocessing tokens, and the macro _x would have been expanded. In this International Standard, line 1 consists of a single preprocessing token, so the macro is not expanded.
2.11
Change: New Keywords
Rationale: Required for new features.
Effect on original feature: Added to Table 3, the following identifiers are new keywords: alignas, alignof, char16_t, char32_t, constexpr, decltype, noexcept, nullptr, static_assert, and thread_local. Valid ISO/IEC 14882:2003 code using these identifiers is invalid in this International Standard.
2.14.2
Change: Type of integer literals
Rationale: C99 compatibility.
Effect on original feature: Certain integer literals larger than can be represented by long could change from an unsigned integer type to signed long long.
C.2.2 Clause 5 expressions
5.6
Change: Specify rounding for results of integer / and %
Rationale: Increase portability, C99.
Effect on original feature: Valid ISO/IEC 14882:2003 code that uses integer division rounds the result toward 0 or toward negative infinity, whereas this International Standard always rounds the result toward 0.
C.2.3 Clause 7 declarations
7.1.1
Change: Remove auto as a storage class specifier
Rationale: New feature.
Effect on original feature: Valid ISO/IEC 14882:2003 code that uses the keyword auto as a storage class specifier may be invalid in this International Standard. In this International Standard, auto now indicates that the type of a variable is to be deduced from its initializer expression.
C.2.4 Clause 8 declarators
8.5.4
Change: Narrowing restrictions in aggregate initializers
Rationale: Catches bugs.
Effect on original feature: Valid ISO/IEC 14882:2003 code may fail to compile in this International Standard.
For example:
The following code is valid in ISO/IEC 14882:2003 but invalid in this International Standard (because double to int is a narrowing conversion):
int x[] = { 2.0 };
C.2.5 Clause 12 special member functions
12.4
Change: User declared destructors have an implicit exception specification.
Rationale: Clarification of destructor requirements.
Effect on original feature: Valid ISO/IEC 14882:2003 code may execute differently in this International Standard. In particular, destructors that throw will call terminate (without calling unexpected) if their exception specification is noexcept or noexcept(true). For a throwing virtual destructor of a derived class, terminate can be avoided only if the base-class virtual destructor has an exception specification that is not noexcept or noexcept(true).
12.1, 12.4, 12.8
Change: Implicitly-declared special member functions are defined as
deleted when the implicit definition would have been ill-formed
Rationale: Improves template argument deduction failure
Effect on original feature: A valid ISO/IEC 14882:2003 program that uses one of these special member functions in a context where the definition is not
required (e.g., in an expression that is not potentially evaluated)
becomes ill-formed.
C.2.6 Clause 14 templates
14.1
Change: Remove export.
Rationale: No implementation consensus.
Effect on original feature: A valid ISO/IEC 14882:2003 declaration containing export is ill-formed in this International Standard.
14.3
Change: Remove whitespace requirement for nested closing template right angle brackets
Rationale: Considered a persistent, but minor, annoyance. Template aliases representing nonclass types would exacerbate whitespace issues.
Effect on original feature: Change to semantics of well-defined expression. A valid ISO/IEC 14882:2003 expression containing a right angle bracket ('>') followed immediately by another right angle bracket may now be treated as closing two templates.
For example:
The following code is valid in ISO/IEC 14882:2003 (because ">>" is a right-shift operator) but invalid in this International Standard (because the ">>" closes two templates):
template<typename T> struct X { };
template<int N> struct Y { };
X< Y< 1 >> 2 > > x;
14.6.4.2
Change: Allow dependent calls of functions with internal linkage.
Rationale: Overly constrained, simplify overload resolution rules.
Effect on original feature: A valid ISO/IEC 14882:2003 program could get a different result than this International Standard.
C.2.7 Clause 17 library introduction
17.6.1.2
Change: New includes.
Rationale: New functionality.
Effect on original feature: The following C++ include files are new: array, atomic, chrono, codecvt, condition_variable, forward_list, future, initializer_list, mutex, random, ratio, regex, scoped_allocator, sytem_error, thread, tuple, typeindex, type_traits, unordered_map, and unordered_set. In addition, the following C compatibility include files are new: ccomplex, cfenv, cinttypes, cstdalign, cstdbool, cstdint, ctgmath, and cuchar. Valid ISO/IEC 14882:2003 code that includes files with these names may be invalid in this International Standard.
17.6.3.2.2
Change: New reserved namespace.
Rationale: New functionality.
Effect on original feature: The global namespace posix is now reserved for standardization. Valid ISO/IEC 14882:2003 code that used a top-level posix namespace may be invalid in this International Standard.
17.6.4.3.1
Change: More restrictions on macro names.
Rationale: Avoid hard to diagnose or non-portable constructs.
Effect on original feature: Attribute identifiers are not allowed to be macros. Valid ISO/IEC 14882:2003 code that defines override, final, carries_dependency, and noreturn as macros is invalid in this International Standard.
17-30
Change: New reserved identifiers.
Rationale: Required by new features.
Effect on original feature: Valid ISO/IEC 14882:2003 code that uses any identifiers used by newly-standardized this International Standard features will fail to compile. A comprehensive list of new identifiers is not provided: instead, consult the index of library names.
C.2.8 Clause 18 library support
18.6.1.1
Change: Linking new/delete operators.
Rationale: The two throwing single-object signatures of operator new and delete are now specified to form the base functionality for the other operators. This clarifies that replacing just these two signatures changes others, even if they are not explicitly changed.
Effect on original feature: Valid ISO/IEC 14882:2003 code that replaces global new/delete operators may execute differently in this International Standard.
For example:
#include <cstdio>
#include <cstdlib>
#include <new>
void* operator new(std::size_t size) throw(std::bad_alloc)
{
…
}
void operator delete(void* ptr) throw()
{
std::printf("custom deallocation\n");
std::free(ptr);
}
int main()
{
int* i = new int;
delete i;
int* a = new int[3]; // Use array version
delete [] a;
return 0;
}
18.6.1.1
Change: Operator new may throw exceptions besides bad_alloc.
Rationale: Consistent application of noexcept.
Effect on original feature: Valid ISO/IEC 14882:2003 code that assumes global operator new only throws bad_alloc may execute differently in this International Standard.
C.2.9 Clause 19 diagnostics
19.4
Change: Thread-local error numbers.
Rationale: Support new thread facilities.
Effect on original feature: Valid but implementation-defined ISO/IEC 14882:2003 code that relies on errno being the same across threads may change behavior in this International Standard.
C.2.10 Clause 20 utilities
20.2.2
Change: Function swap changes headers.
Rationale: Remove dependency on <algorithm> for swap.
Effect on original feature: Valid ISO/IEC 14882:2003 code that has been compiled expecting swap to be in <algorithm> may have to instead include <utility>.
20.6.4
Change: Minimal support for garbage-collected regions.
Rationale: Required by new feature.
Effect on original feature: Valid ISO/IEC 14882:2003 code that has been compiled without traceable pointer support and then inter-operates with newer C++ code using regions declared reachable may experience different or undefined runtime behavior.
20.8.4
20.8.5
20.8.6
20.8.7
20.8.8
Change: Standard function objects no longer derived.
Rationale: Superseded by new feature.
Effect on original feature: Valid ISO/IEC 14882:2003 code that depends on function objects being derived from unary_function or binary_function will execute differently in this International Standard.
C.2.11 Clause 21 strings
21.3
Change: Change basic_string requirements to disallow reference counted strings.
Rationale: Invalidation is subtly different between reference-counted strings and other implementation strategies. Regularize behavior for new this International Standard features.
Effect on original feature: ISO/IEC 14882:2003 code may execute differently in this International Standard.
21.4.1
Change: Loosen basic_string invalidation requirements.
Rationale: Allow implementations to use a small-string optimization
Effect on original feature: ISO/IEC 14882:2003 code may execute differently in this International Standard. Some const member functions, like data and c_str, no longer invalidate iterators.
C.2.12 Clause 23 containers
23.2
Change: Requirements change: specify size() complexity to be constant.
Rationale: Lack of size() specification results in divergent implementations with inconsistent performance characteristics.
Effect on original feature: Some container implementations in ISO/IEC 14882:2003 may not conform to the specified size() requirements in this International Standard. Adjusting containers (such as list) to the stricter requirements may require incompatible changes.
23.2
Change: Requirements change: relaxation.
Rationale: Clarification of container requirements.
Effect on original feature: Valid ISO/IEC 14882:2003 code that attempted to meet the specified container requirements may now be over-specified. Code that attempted to be portable across containers may need to be adjusted as follows:
a) not all containers provide size() so use empty() instead of size()==0
b) not all containers are empty after construction (array)
c) not all containers have constant complexity with swap() (array)
23.2
Change: Requirements change: default constructible.
Rationale: Clarification of container requirements.
Effect on original feature: Valid ISO/IEC 14882:2003 code that attempted to explicitly instantiate a container using a user-defined type with no default constructor may fail to compile.
23.2.3
23.2.4
Change: Signature changes: from void return types.
Rationale: Throwing away useful information that may be expensive to re-calculate.
Effect on original feature: The following member functions changed:
a) erase(iter) for set, multiset, map, multimap
b) erase(begin,end) for set, multiset, map, multimap
c) insert(pos,num,val) for vector, deque, list, forward_list
d) insert(pos,beg,end) for vector, deque, list, forward_list
Valid ISO/IEC 14882:2003 code that may not be inter-operable with this International Standard if return types change from void.
23.2.3
23.2.4
Change: Signature changes: from iterator to const_iterator parameters.
Rationale: Overspecification.
Effect on original feature: The following member functions changed from iterator to const_iterator:
a) insert(iter,val) for vector, deque, list, set, multiset, map, multimap
b) insert(pos,beg,end) for vector, deque, list, forward_list
c) erase(iter) for set, multiset, map, multimap
d) erase(begin,end) for set, multiset, map, multimap
e) all forms of list::splice
f) all forms of list::merge
Valid ISO/IEC 14882:2003 code that may not be inter-operable with this International Standard if member function parameters change.
23.2.3
23.2.4
Change: Signature changes: resize.
Rationale: Performance, compatibility with move semantics.
Effect on original feature: The resize member function changed from value to reference, and an additional overload was added. This impacts vector, deque, and list. Valid ISO/IEC 14882:2003 code may not be inter-operable with this International Standard if member function parameters change.
C.2.13 Clause 25 algorithms
25.1
Change: Result state of inputs after algorithm use.
Rationale: Required by new features.
Effect on original feature: A ISO/IEC 14882:2003 program may notice that an object with a valid but unspecified state has a different valid but unspecified state. Specific algorithms of note are remove and remove_if, but may not be limited to just these two examples.
C.2.14 Clause 26 numerics
26.4
Change: Specify complex representation.
Rationale: Compatibility with C99.
Effect on original feature: Valid ISO/IEC 14882:2003 code that uses implementation-specific knowledge about the binary representation of the required template specializations of complex may not be compatible with this International Standard, which mandates storage-compatibility with C99.
C.2.15 Clause 27 io
27.7.2.1.3
27.7.3.4
27.5.5.4
Change: Specify use of explicit in existing boolean conversion operators
Rationale: Clarify intentions, avoid workarounds.
Effect on original feature: Use 'explicit operator bool()' rather than unspecified bool type or void* for conversions in basic_ios, basic_istream, and basic_ostream sentries. The lack of implicit conversion-to-bool will break code relying on it in the following conditions:
a) Passing into a 'bool' argument for a function
b) Invoking operator==
c) In a return statement for a function returning bool
d) Initializing 'bool' members in an aggregate via aggregate initialization
e) Initializing a 'const bool &' which would bind to the temporary
27.4.2
Change: Change base class of ios_base::failure.
Rationale: Detailed error messages.
Effect on original feature: Define ios_base::failure base class as system_error. ISO/IEC 14882:2003 code may execute differently in this International Standard.
27.5.2
Change: Change ios_base:: types from enumerations to formalized bitmasks represented as constexpr member data.
Rationale: All bitmask types should be defined as enumerations with mask operators implemented as constexpr functions.
Effect on original feature: Define ios_base::fmtflags, ios_base::iostate, ios_base::openmode, ios_base::seekdir as formalized bitmask types.
Some code that depended on the implementation properties of these types being std::bitset or a builtin integer type may no longer compile.
For example:
#include <iostream>
std::cout.setf(17); // should be std::cout.setf(hex);
References
N2270 Incompatible Changes in this International Standard
N2733 Appendix C: ISO C++ 2003 Compatibility
N3126 Draft Programing Languages - C++, Pete Becker
N3204 Deducing 'noexcept' for destructors
N3205 Delete operators default to noexcept
N3110 Problems with bitmask types in the library
N3064 Core issue 374: Explicit specialization outside a template's parent (revision 1)
N3065 Removing Export
N3068 Equality Comparison for Unordered Containers (Rev 2)
N2923 Specifying the complexity of size() (Revision 1)
N2760 Input/Output Library Thread Safety
N2769 Detailed Reporting for Input/Output Library Errors (Revision 2)
N2668 Concurrency Modifications to Basic String
N2680 Proposed Wording for Placement Insert (Revision 1)
N2586 N2586: Minimal Support for Garbage Collection and Reachability-Based Leak Detection (revised)
N1388 Enhancing Numerical Support
N1811 Adding the long long type to C++ (Revision 3)
Acknowledgements
LWG meeting attendents, Alisdair Meredith, Daniel Krügler, Nicolai M. Josuttis, Jason Merrill, John Spicer, Doug Gregor, Jan Stephen Adamczyk, Ville Voutilainen, William Michael Miller