- tmp/tmp2zgj9ype/{from.md → to.md} +200 -70
tmp/tmp2zgj9ype/{from.md → to.md}
RENAMED
|
@@ -1,37 +1,43 @@
|
|
| 1 |
-
## C++ and
|
| 2 |
|
| 3 |
### General <a id="diff.iso.general">[[diff.iso.general]]</a>
|
| 4 |
|
| 5 |
-
Subclause [[diff.iso]] lists the differences between C++ and
|
| 6 |
addition to those listed above, by the chapters of this document.
|
| 7 |
|
| 8 |
### [[lex]]: lexical conventions <a id="diff.lex">[[diff.lex]]</a>
|
| 9 |
|
| 10 |
-
**Change:** New Keywords
|
| 11 |
New keywords are added to C++; see [[lex.key]]. **Rationale:** These
|
| 12 |
keywords were added in order to implement the new semantics of C++.
|
| 13 |
**Effect on original feature:** Change to semantics of well-defined
|
| 14 |
-
feature. Any
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
**Change:** Type of *character-literal* is changed from `int` to `char`.
|
| 20 |
**Rationale:** This is needed for improved overloaded function argument
|
| 21 |
-
type matching.
|
|
|
|
|
|
|
| 22 |
|
| 23 |
``` cpp
|
| 24 |
int function( int i );
|
| 25 |
int function( char c );
|
| 26 |
|
| 27 |
function( 'x' );
|
| 28 |
```
|
| 29 |
|
| 30 |
It is preferable that this call match the second version of function
|
| 31 |
-
rather than the first.
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
sizeof('x') == sizeof(int)
|
| 36 |
```
|
| 37 |
|
|
@@ -71,46 +77,62 @@ Programs that have a legitimate reason to treat string literal objects
|
|
| 71 |
as potentially modifiable memory are probably rare.
|
| 72 |
|
| 73 |
### [[basic]]: basics <a id="diff.basic">[[diff.basic]]</a>
|
| 74 |
|
| 75 |
**Change:** C++ does not have “tentative definitions” as in C.
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
``` cpp
|
| 79 |
int i;
|
| 80 |
int i;
|
| 81 |
```
|
| 82 |
|
| 83 |
-
is valid in C, invalid in C++.
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
``` cpp
|
| 88 |
struct X { int i; struct X* next; };
|
| 89 |
|
| 90 |
static struct X a;
|
| 91 |
static struct X b = { 0, &a };
|
| 92 |
static struct X a = { 1, &b };
|
| 93 |
```
|
| 94 |
|
|
|
|
|
|
|
| 95 |
**Rationale:** This avoids having different initialization rules for
|
| 96 |
fundamental types and user-defined types. **Effect on original
|
| 97 |
feature:** Deletion of semantically well-defined feature. Semantic
|
| 98 |
transformation. In C++, the initializer for one of a set of
|
| 99 |
mutually-referential file-local objects with static storage duration
|
| 100 |
must invoke a function call to achieve the initialization. Seldom.
|
| 101 |
|
| 102 |
-
**Change:** A `struct` is a scope in C++, not in C.
|
|
|
|
|
|
|
| 103 |
|
| 104 |
``` cpp
|
| 105 |
struct X {
|
| 106 |
struct Y { int a; } b;
|
| 107 |
};
|
| 108 |
struct Y c;
|
| 109 |
```
|
| 110 |
|
| 111 |
is valid in C but not in C++, which would require `X::Y c;`.
|
|
|
|
|
|
|
|
|
|
| 112 |
**Rationale:** Class scope is crucial to C++, and a struct is a class.
|
| 113 |
**Effect on original feature:** Change to semantics of well-defined
|
| 114 |
feature. Semantic transformation. C programs use `struct` extremely
|
| 115 |
frequently, but the change is only noticeable when `struct`,
|
| 116 |
enumeration, or enumerator names are referred to outside the `struct`.
|
|
@@ -143,73 +165,145 @@ many, but not all, of such problems. Those problems not found by
|
|
| 143 |
typesafe linkage will continue to function properly, according to the
|
| 144 |
“layout compatibility rules” of this document. Common.
|
| 145 |
|
| 146 |
### [[expr]]: expressions <a id="diff.expr">[[diff.expr]]</a>
|
| 147 |
|
| 148 |
-
**Change:** Converting
|
| 149 |
-
casting.
|
|
|
|
|
|
|
| 150 |
|
| 151 |
``` cpp
|
| 152 |
char a[10];
|
| 153 |
void* b=a;
|
| 154 |
void foo() {
|
| 155 |
char* c=b;
|
| 156 |
}
|
| 157 |
```
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
``` cpp
|
| 167 |
char* c = (char*) b;
|
| 168 |
```
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
**Change:** Decrement operator is not allowed with `bool` operand.
|
| 175 |
**Rationale:** Feature with surprising semantics. **Effect on original
|
| 176 |
-
feature:** A valid
|
| 177 |
-
|
| 178 |
ill-formed in C++.
|
| 179 |
|
| 180 |
**Change:** In C++, types can only be defined in declarations, not in
|
| 181 |
expressions.
|
| 182 |
In C, a `sizeof` expression or cast expression may define a new type.
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
``` cpp
|
| 186 |
p = (void*)(struct x {int i;} *)0;
|
| 187 |
```
|
| 188 |
|
| 189 |
-
defines a new type, struct `x`.
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
**Change:** The result of a conditional expression, an assignment
|
| 195 |
expression, or a comma expression may be an lvalue. **Rationale:** C++
|
| 196 |
is an object-oriented language, placing relatively more emphasis on
|
| 197 |
lvalues. For example, function calls may yield lvalues. **Effect on
|
| 198 |
original feature:** Change to semantics of well-defined feature. Some C
|
| 199 |
expressions that implicitly rely on lvalue-to-rvalue conversions will
|
| 200 |
-
yield different results.
|
|
|
|
|
|
|
| 201 |
|
| 202 |
``` cpp
|
| 203 |
char arr[100];
|
| 204 |
sizeof(0, arr)
|
| 205 |
```
|
| 206 |
|
| 207 |
-
yields `100` in C++ and `sizeof(char*)` in C.
|
| 208 |
-
casts to the appropriate rvalue. Rare.
|
| 209 |
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
**Change:** It is now invalid to jump past a declaration with explicit
|
| 213 |
or implicit initializer (except across entire block not entered).
|
| 214 |
**Rationale:** Constructors used in initializers may allocate resources
|
| 215 |
which need to be de-allocated upon leaving the block. Allowing jump past
|
|
@@ -233,25 +327,27 @@ with `void` and `int` return types. **Effect on original feature:**
|
|
| 233 |
Deletion of semantically well-defined feature. Semantic transformation.
|
| 234 |
Add an appropriate return value to the source code, such as zero.
|
| 235 |
Seldom. For several years, many existing C implementations have produced
|
| 236 |
warnings in this case.
|
| 237 |
|
| 238 |
-
### [[dcl
|
| 239 |
|
| 240 |
**Change:** In C++, the `static` or `extern` specifiers can only be
|
| 241 |
applied to names of objects or functions.
|
| 242 |
Using these specifiers with type declarations is illegal in C++. In C,
|
| 243 |
these specifiers are ignored when used on type declarations.
|
| 244 |
|
| 245 |
-
Example:
|
| 246 |
|
| 247 |
``` cpp
|
| 248 |
static struct S { // valid C, invalid in C++{}
|
| 249 |
int i;
|
| 250 |
};
|
| 251 |
```
|
| 252 |
|
|
|
|
|
|
|
| 253 |
**Rationale:** Storage class specifiers don’t have any meaning when
|
| 254 |
associated with a type. In C++, class members can be declared with the
|
| 255 |
`static` storage class specifier. Storage class specifiers on type
|
| 256 |
declarations can be confusing for users. **Effect on original feature:**
|
| 257 |
Deletion of semantically well-defined feature. Syntactic transformation.
|
|
@@ -266,29 +362,33 @@ feature. Syntactic transformation. Common.
|
|
| 266 |
name declared in the same scope (except if the typedef is a synonym of
|
| 267 |
the class name with the same name). In C, a *typedef-name* and a struct
|
| 268 |
tag name declared in the same scope can have the same name (because they
|
| 269 |
have different name spaces).
|
| 270 |
|
| 271 |
-
Example:
|
| 272 |
|
| 273 |
``` cpp
|
| 274 |
typedef struct name1 { ... } name1; // valid C and C++{}
|
| 275 |
struct name { ... };
|
| 276 |
typedef int name; // valid C, invalid C++{}
|
| 277 |
```
|
| 278 |
|
|
|
|
|
|
|
| 279 |
**Rationale:** For ease of use, C++ doesn’t require that a type name be
|
| 280 |
prefixed with the keywords `class`, `struct` or `union` when used in
|
| 281 |
object declarations or type casts.
|
| 282 |
|
| 283 |
-
Example:
|
| 284 |
|
| 285 |
``` cpp
|
| 286 |
class name { ... };
|
| 287 |
name i; // i has type class name
|
| 288 |
```
|
| 289 |
|
|
|
|
|
|
|
| 290 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 291 |
feature. Semantic transformation. One of the 2 types has to be renamed.
|
| 292 |
Seldom.
|
| 293 |
|
| 294 |
\[see also [[basic.link]]\] **Change:** Const objects must be
|
|
@@ -298,55 +398,60 @@ useful value. **Effect on original feature:** Deletion of semantically
|
|
| 298 |
well-defined feature. Semantic transformation. Seldom.
|
| 299 |
|
| 300 |
**Change:** The keyword `auto` cannot be used as a storage class
|
| 301 |
specifier.
|
| 302 |
|
| 303 |
-
Example:
|
| 304 |
|
| 305 |
``` cpp
|
| 306 |
void f() {
|
| 307 |
auto int x; // valid C, invalid C++{}
|
| 308 |
}
|
| 309 |
```
|
| 310 |
|
|
|
|
|
|
|
| 311 |
**Rationale:** Allowing the use of `auto` to deduce the type of a
|
| 312 |
variable from its initializer results in undesired interpretations of
|
| 313 |
`auto` as a storage class specifier in certain contexts. **Effect on
|
| 314 |
original feature:** Deletion of semantically well-defined feature.
|
| 315 |
Syntactic transformation. Rare.
|
| 316 |
|
| 317 |
**Change:** In C++, a function declared with an empty parameter list
|
| 318 |
takes no arguments. In C, an empty parameter list means that the number
|
| 319 |
and type of the function arguments are unknown.
|
| 320 |
|
| 321 |
-
Example:
|
| 322 |
|
| 323 |
``` cpp
|
| 324 |
int f(); // means int f(void) in C++{}
|
| 325 |
// int f( unknown ) in C
|
| 326 |
```
|
| 327 |
|
| 328 |
-
*
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
| 337 |
|
| 338 |
\[see [[expr.sizeof]]\] **Change:** In C++, types may not be defined in
|
| 339 |
return or parameter types. In C, these type definitions are allowed.
|
| 340 |
|
| 341 |
-
Example:
|
| 342 |
|
| 343 |
``` cpp
|
| 344 |
void f( struct S { int a; } arg ) {} // valid C, invalid C++{}
|
| 345 |
enum E { A, B, C } f() {} // valid C, invalid C++{}
|
| 346 |
```
|
| 347 |
|
|
|
|
|
|
|
| 348 |
**Rationale:** When comparing types in different translation units, C++
|
| 349 |
relies on name equivalence when C relies on structural equivalence.
|
| 350 |
Regarding parameter types: since the type defined in a parameter list
|
| 351 |
would be in the scope of the function, the only legal calls in C++ would
|
| 352 |
be from within the function itself. **Effect on original feature:**
|
|
@@ -366,24 +471,26 @@ compared to the corresponding functionality in C. In C++, designators
|
|
| 366 |
for non-static data members must be specified in declaration order,
|
| 367 |
designators for array elements and nested designators are not supported,
|
| 368 |
and designated and non-designated initializers cannot be mixed in the
|
| 369 |
same initializer list.
|
| 370 |
|
| 371 |
-
Example:
|
| 372 |
|
| 373 |
``` cpp
|
| 374 |
struct A { int x, y; };
|
| 375 |
struct B { struct A a; };
|
| 376 |
struct A a = {.y = 1, .x = 2}; // valid C, invalid C++{}
|
| 377 |
int arr[3] = {[1] = 5}; // valid C, invalid C++{}
|
| 378 |
struct B b = {.a.x = 0}; // valid C, invalid C++{}
|
| 379 |
struct A c = {.x = 1, 2}; // valid C, invalid C++{}
|
| 380 |
```
|
| 381 |
|
|
|
|
|
|
|
| 382 |
**Rationale:** In C++, members are destroyed in reverse construction
|
| 383 |
order and the elements of an initializer list are evaluated in lexical
|
| 384 |
-
order, so
|
| 385 |
designators conflict with *lambda-expression* syntax. Nested designators
|
| 386 |
are seldom used. **Effect on original feature:** Deletion of feature
|
| 387 |
that is incompatible with C++. Syntactic transformation. Out-of-order
|
| 388 |
initializers are common. The other features are seldom used.
|
| 389 |
|
|
@@ -391,16 +498,18 @@ initializers are common. The other features are seldom used.
|
|
| 391 |
string, the number of characters in the string (including the
|
| 392 |
terminating `'\0'`) must not exceed the number of elements in the array.
|
| 393 |
In C, an array can be initialized with a string even if the array is not
|
| 394 |
large enough to contain the string-terminating `'\0'`.
|
| 395 |
|
| 396 |
-
Example:
|
| 397 |
|
| 398 |
``` cpp
|
| 399 |
char array[4] = "abcd"; // valid C, invalid C++{}
|
| 400 |
```
|
| 401 |
|
|
|
|
|
|
|
| 402 |
**Rationale:** When these non-terminated arrays are manipulated by
|
| 403 |
standard string functions, there is potential for major catastrophe.
|
| 404 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 405 |
feature. Semantic transformation. The arrays must be declared one
|
| 406 |
element bigger to contain the string terminating `'\0'`. Seldom. This
|
|
@@ -408,51 +517,57 @@ style of array initialization is seen as poor coding style.
|
|
| 408 |
|
| 409 |
**Change:** C++ objects of enumeration type can only be assigned values
|
| 410 |
of the same enumeration type. In C, objects of enumeration type can be
|
| 411 |
assigned values of any integral type.
|
| 412 |
|
| 413 |
-
Example:
|
| 414 |
|
| 415 |
``` cpp
|
| 416 |
enum color { red, blue, green };
|
| 417 |
enum color c = 1; // valid C, invalid C++{}
|
| 418 |
```
|
| 419 |
|
|
|
|
|
|
|
| 420 |
**Rationale:** The type-safe nature of C++. **Effect on original
|
| 421 |
feature:** Deletion of semantically well-defined feature. Syntactic
|
| 422 |
transformation. (The type error produced by the assignment can be
|
| 423 |
automatically corrected by applying an explicit cast.) Common.
|
| 424 |
|
| 425 |
**Change:** In C++, the type of an enumerator is its enumeration. In C,
|
| 426 |
the type of an enumerator is `int`.
|
| 427 |
|
| 428 |
-
Example:
|
| 429 |
|
| 430 |
``` cpp
|
| 431 |
enum e { A };
|
| 432 |
sizeof(A) == sizeof(int) // in C
|
| 433 |
sizeof(A) == sizeof(e) // in C++{}
|
| 434 |
/* and sizeof(int) is not necessarily equal to sizeof(e) */
|
| 435 |
```
|
| 436 |
|
|
|
|
|
|
|
| 437 |
**Rationale:** In C++, an enumeration is a distinct type. **Effect on
|
| 438 |
original feature:** Change to semantics of well-defined feature.
|
| 439 |
Semantic transformation. Seldom. The only time this affects existing C
|
| 440 |
code is when the size of an enumerator is taken. Taking the size of an
|
| 441 |
enumerator is not a common C coding practice.
|
| 442 |
|
| 443 |
**Change:** In C++, an *alignment-specifier* is an
|
| 444 |
*attribute-specifier*. In C, an *alignment-specifier* is a .
|
| 445 |
|
| 446 |
-
Example:
|
| 447 |
|
| 448 |
``` cpp
|
| 449 |
#include <stdalign.h>
|
| 450 |
unsigned alignas(8) int x; // valid C, invalid C++{}
|
| 451 |
unsigned int y alignas(8); // valid C++{}, invalid C
|
| 452 |
```
|
| 453 |
|
|
|
|
|
|
|
| 454 |
**Rationale:** C++ requires unambiguous placement of the
|
| 455 |
*alignment-specifier*. **Effect on original feature:** Deletion of
|
| 456 |
semantically well-defined feature. Syntactic transformation. Seldom.
|
| 457 |
|
| 458 |
### [[class]]: classes <a id="diff.class">[[diff.class]]</a>
|
|
@@ -461,21 +576,23 @@ semantically well-defined feature. Syntactic transformation. Seldom.
|
|
| 461 |
introduces the class name into the scope where it is declared and hides
|
| 462 |
any object, function or other declaration of that name in an enclosing
|
| 463 |
scope. In C, an inner scope declaration of a struct tag name never hides
|
| 464 |
the name of an object or function in an outer scope.
|
| 465 |
|
| 466 |
-
Example:
|
| 467 |
|
| 468 |
``` cpp
|
| 469 |
int x[99];
|
| 470 |
void f() {
|
| 471 |
struct x { int a; };
|
| 472 |
sizeof(x); /* size of the array in C */
|
| 473 |
/* size of the struct in \textit{\textrm{C++{}}} */
|
| 474 |
}
|
| 475 |
```
|
| 476 |
|
|
|
|
|
|
|
| 477 |
**Rationale:** This is one of the few incompatibilities between C and
|
| 478 |
C++ that can be attributed to the new C++ name space definition where a
|
| 479 |
name can be declared as a type and as a non-type in a single scope
|
| 480 |
causing the non-type name to hide the type name and requiring that the
|
| 481 |
keywords `class`, `struct`, `union` or `enum` be used to refer to the
|
|
@@ -491,21 +608,26 @@ is at block scope, either the type or the struct tag has to be renamed.
|
|
| 491 |
Seldom.
|
| 492 |
|
| 493 |
**Change:** Copying volatile objects.
|
| 494 |
|
| 495 |
The implicitly-declared copy constructor and implicitly-declared copy
|
| 496 |
-
assignment operator cannot make a copy of a volatile lvalue.
|
| 497 |
-
|
|
|
|
|
|
|
|
|
|
| 498 |
|
| 499 |
``` cpp
|
| 500 |
struct X { int i; };
|
| 501 |
volatile struct X x1 = {0};
|
| 502 |
struct X x2 = x1; // invalid C++{}
|
| 503 |
struct X x3;
|
| 504 |
x3 = x1; // also invalid C++{}
|
| 505 |
```
|
| 506 |
|
|
|
|
|
|
|
| 507 |
**Rationale:** Several alternatives were debated at length. Changing the
|
| 508 |
parameter to `volatile` `const` `X&` would greatly complicate the
|
| 509 |
generation of efficient code for class objects. Discussion of providing
|
| 510 |
two alternative signatures for these implicitly-defined operations
|
| 511 |
raised unanswered concerns about creating ambiguities and complicating
|
|
@@ -525,68 +647,76 @@ transformation. Seldom.
|
|
| 525 |
|
| 526 |
**Change:** In C++, the name of a nested class is local to its enclosing
|
| 527 |
class. In C the name of the nested class belongs to the same scope as
|
| 528 |
the name of the outermost enclosing class.
|
| 529 |
|
| 530 |
-
Example:
|
| 531 |
|
| 532 |
``` cpp
|
| 533 |
struct X {
|
| 534 |
struct Y { ... } y;
|
| 535 |
};
|
| 536 |
struct Y yy; // valid C, invalid C++{}
|
| 537 |
```
|
| 538 |
|
|
|
|
|
|
|
| 539 |
**Rationale:** C++ classes have member functions which require that
|
| 540 |
classes establish scopes. The C rule would leave classes as an
|
| 541 |
incomplete scope mechanism which would prevent C++ programmers from
|
| 542 |
maintaining locality within a class. A coherent set of scope rules for
|
| 543 |
C++ based on the C rule would be very complicated and C++ programmers
|
| 544 |
would be unable to predict reliably the meanings of nontrivial examples
|
| 545 |
involving nested or local functions. **Effect on original feature:**
|
| 546 |
Change to semantics of well-defined feature. Semantic transformation. To
|
| 547 |
make the struct type name visible in the scope of the enclosing struct,
|
| 548 |
the struct tag can be declared in the scope of the enclosing struct,
|
| 549 |
-
before the enclosing struct is defined.
|
|
|
|
|
|
|
| 550 |
|
| 551 |
``` cpp
|
| 552 |
struct Y; // struct Y and struct X are at the same scope
|
| 553 |
struct X {
|
| 554 |
struct Y { ... } y;
|
| 555 |
};
|
| 556 |
```
|
| 557 |
|
|
|
|
|
|
|
| 558 |
All the definitions of C struct types enclosed in other struct
|
| 559 |
definitions and accessed outside the scope of the enclosing struct can
|
| 560 |
be exported to the scope of the enclosing struct. Note: this is a
|
| 561 |
consequence of the difference in scope rules, which is documented in
|
| 562 |
[[basic.scope]]. Seldom.
|
| 563 |
|
| 564 |
**Change:** In C++, a *typedef-name* may not be redeclared in a class
|
| 565 |
definition after being used in that definition.
|
| 566 |
|
| 567 |
-
Example:
|
| 568 |
|
| 569 |
``` cpp
|
| 570 |
typedef int I;
|
| 571 |
struct S {
|
| 572 |
I i;
|
| 573 |
int I; // valid C, invalid C++{}
|
| 574 |
};
|
| 575 |
```
|
| 576 |
|
|
|
|
|
|
|
| 577 |
**Rationale:** When classes become complicated, allowing such a
|
| 578 |
redefinition after the type has been used can create confusion for C++
|
| 579 |
programmers as to what the meaning of `I` really is. **Effect on
|
| 580 |
original feature:** Deletion of semantically well-defined feature.
|
| 581 |
Semantic transformation. Either the type or the struct member has to be
|
| 582 |
renamed. Seldom.
|
| 583 |
|
| 584 |
### [[cpp]]: preprocessing directives <a id="diff.cpp">[[diff.cpp]]</a>
|
| 585 |
|
| 586 |
**Change:** Whether `__STDC__` is defined and if so, what its value is,
|
| 587 |
-
are *implementation-defined*. **Rationale:** C++ is not identical to
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
|
|
|
|
| 1 |
+
## C++ and C <a id="diff.iso">[[diff.iso]]</a>
|
| 2 |
|
| 3 |
### General <a id="diff.iso.general">[[diff.iso.general]]</a>
|
| 4 |
|
| 5 |
+
Subclause [[diff.iso]] lists the differences between C++ and C, in
|
| 6 |
addition to those listed above, by the chapters of this document.
|
| 7 |
|
| 8 |
### [[lex]]: lexical conventions <a id="diff.lex">[[diff.lex]]</a>
|
| 9 |
|
| 10 |
+
**Change:** New Keywords.
|
| 11 |
New keywords are added to C++; see [[lex.key]]. **Rationale:** These
|
| 12 |
keywords were added in order to implement the new semantics of C++.
|
| 13 |
**Effect on original feature:** Change to semantics of well-defined
|
| 14 |
+
feature. Any C programs that used any of these keywords as identifiers
|
| 15 |
+
are not valid C++ programs. Syntactic transformation. Converting one
|
| 16 |
+
specific program is easy. Converting a large collection of related
|
| 17 |
+
programs takes more work. Common.
|
| 18 |
|
| 19 |
**Change:** Type of *character-literal* is changed from `int` to `char`.
|
| 20 |
**Rationale:** This is needed for improved overloaded function argument
|
| 21 |
+
type matching.
|
| 22 |
+
|
| 23 |
+
[*Example 1*:
|
| 24 |
|
| 25 |
``` cpp
|
| 26 |
int function( int i );
|
| 27 |
int function( char c );
|
| 28 |
|
| 29 |
function( 'x' );
|
| 30 |
```
|
| 31 |
|
| 32 |
It is preferable that this call match the second version of function
|
| 33 |
+
rather than the first.
|
| 34 |
+
|
| 35 |
+
— *end example*]
|
| 36 |
+
|
| 37 |
+
**Effect on original feature:** Change to semantics of well-defined
|
| 38 |
+
feature. C programs which depend on
|
| 39 |
|
| 40 |
``` cpp
|
| 41 |
sizeof('x') == sizeof(int)
|
| 42 |
```
|
| 43 |
|
|
|
|
| 77 |
as potentially modifiable memory are probably rare.
|
| 78 |
|
| 79 |
### [[basic]]: basics <a id="diff.basic">[[diff.basic]]</a>
|
| 80 |
|
| 81 |
**Change:** C++ does not have “tentative definitions” as in C.
|
| 82 |
+
|
| 83 |
+
[*Example 1*:
|
| 84 |
+
|
| 85 |
+
At file scope,
|
| 86 |
|
| 87 |
``` cpp
|
| 88 |
int i;
|
| 89 |
int i;
|
| 90 |
```
|
| 91 |
|
| 92 |
+
is valid in C, invalid in C++.
|
| 93 |
+
|
| 94 |
+
— *end example*]
|
| 95 |
+
|
| 96 |
+
This makes it impossible to define mutually referential file-local
|
| 97 |
+
objects with static storage duration, if initializers are restricted to
|
| 98 |
+
the syntactic forms of C.
|
| 99 |
+
|
| 100 |
+
[*Example 2*:
|
| 101 |
|
| 102 |
``` cpp
|
| 103 |
struct X { int i; struct X* next; };
|
| 104 |
|
| 105 |
static struct X a;
|
| 106 |
static struct X b = { 0, &a };
|
| 107 |
static struct X a = { 1, &b };
|
| 108 |
```
|
| 109 |
|
| 110 |
+
— *end example*]
|
| 111 |
+
|
| 112 |
**Rationale:** This avoids having different initialization rules for
|
| 113 |
fundamental types and user-defined types. **Effect on original
|
| 114 |
feature:** Deletion of semantically well-defined feature. Semantic
|
| 115 |
transformation. In C++, the initializer for one of a set of
|
| 116 |
mutually-referential file-local objects with static storage duration
|
| 117 |
must invoke a function call to achieve the initialization. Seldom.
|
| 118 |
|
| 119 |
+
**Change:** A `struct` is a scope in C++, not in C.
|
| 120 |
+
|
| 121 |
+
[*Example 3*:
|
| 122 |
|
| 123 |
``` cpp
|
| 124 |
struct X {
|
| 125 |
struct Y { int a; } b;
|
| 126 |
};
|
| 127 |
struct Y c;
|
| 128 |
```
|
| 129 |
|
| 130 |
is valid in C but not in C++, which would require `X::Y c;`.
|
| 131 |
+
|
| 132 |
+
— *end example*]
|
| 133 |
+
|
| 134 |
**Rationale:** Class scope is crucial to C++, and a struct is a class.
|
| 135 |
**Effect on original feature:** Change to semantics of well-defined
|
| 136 |
feature. Semantic transformation. C programs use `struct` extremely
|
| 137 |
frequently, but the change is only noticeable when `struct`,
|
| 138 |
enumeration, or enumerator names are referred to outside the `struct`.
|
|
|
|
| 165 |
typesafe linkage will continue to function properly, according to the
|
| 166 |
“layout compatibility rules” of this document. Common.
|
| 167 |
|
| 168 |
### [[expr]]: expressions <a id="diff.expr">[[diff.expr]]</a>
|
| 169 |
|
| 170 |
+
**Change:** Converting a prvalue of type “pointer to cv `void`” to a
|
| 171 |
+
pointer-to-object type requires casting.
|
| 172 |
+
|
| 173 |
+
[*Example 1*:
|
| 174 |
|
| 175 |
``` cpp
|
| 176 |
char a[10];
|
| 177 |
void* b=a;
|
| 178 |
void foo() {
|
| 179 |
char* c=b;
|
| 180 |
}
|
| 181 |
```
|
| 182 |
|
| 183 |
+
C accepts this usage of “pointer to `void`” being assigned to a pointer
|
| 184 |
+
to object type. C++ does not.
|
| 185 |
+
|
| 186 |
+
— *end example*]
|
| 187 |
+
|
| 188 |
+
**Rationale:** C++ tries harder than C to enforce compile-time type
|
| 189 |
+
safety. **Effect on original feature:** Deletion of semantically
|
| 190 |
+
well-defined feature. Can be automated. Violations will be diagnosed by
|
| 191 |
+
the C++ translator. The fix is to add a cast.
|
| 192 |
+
|
| 193 |
+
[*Example 2*:
|
| 194 |
|
| 195 |
``` cpp
|
| 196 |
char* c = (char*) b;
|
| 197 |
```
|
| 198 |
|
| 199 |
+
— *end example*]
|
| 200 |
+
|
| 201 |
+
Common.
|
| 202 |
+
|
| 203 |
+
**Change:** Operations mixing a value of an enumeration type and a value
|
| 204 |
+
of a different enumeration type or of a floating-point type are not
|
| 205 |
+
valid.
|
| 206 |
+
|
| 207 |
+
[*Example 3*:
|
| 208 |
+
|
| 209 |
+
``` cpp
|
| 210 |
+
enum E1 { e };
|
| 211 |
+
enum E2 { f };
|
| 212 |
+
int b = e <= 3.7; // valid in C; ill-formed in C++{}
|
| 213 |
+
int k = f - e; // valid in C; ill-formed in C++{}
|
| 214 |
+
int x = 1 ? e : f; // valid in C; ill-formed in C++{}
|
| 215 |
+
```
|
| 216 |
+
|
| 217 |
+
— *end example*]
|
| 218 |
+
|
| 219 |
+
**Rationale:** Reinforcing type safety in C++. **Effect on original
|
| 220 |
+
feature:** Well-formed C code will not compile with this International
|
| 221 |
+
Standard. Violations will be diagnosed by the C++ translator. The
|
| 222 |
+
original behavior can be restored with a cast or integral promotion.
|
| 223 |
+
|
| 224 |
+
[*Example 4*:
|
| 225 |
+
|
| 226 |
+
``` cpp
|
| 227 |
+
enum E1 { e };
|
| 228 |
+
enum E2 { f };
|
| 229 |
+
int b = (int)e <= 3.7;
|
| 230 |
+
int k = +f - e;
|
| 231 |
+
```
|
| 232 |
+
|
| 233 |
+
— *end example*]
|
| 234 |
+
|
| 235 |
+
Uncommon.
|
| 236 |
|
| 237 |
**Change:** Decrement operator is not allowed with `bool` operand.
|
| 238 |
**Rationale:** Feature with surprising semantics. **Effect on original
|
| 239 |
+
feature:** A valid C expression utilizing the decrement operator on a
|
| 240 |
+
`bool` lvalue (for instance, via the C typedef in `<stdbool.h>`) is
|
| 241 |
ill-formed in C++.
|
| 242 |
|
| 243 |
**Change:** In C++, types can only be defined in declarations, not in
|
| 244 |
expressions.
|
| 245 |
In C, a `sizeof` expression or cast expression may define a new type.
|
| 246 |
+
|
| 247 |
+
[*Example 5*:
|
| 248 |
|
| 249 |
``` cpp
|
| 250 |
p = (void*)(struct x {int i;} *)0;
|
| 251 |
```
|
| 252 |
|
| 253 |
+
defines a new type, struct `x`.
|
| 254 |
+
|
| 255 |
+
— *end example*]
|
| 256 |
+
|
| 257 |
+
**Rationale:** This prohibition helps to clarify the location of
|
| 258 |
+
definitions in the source code. **Effect on original feature:** Deletion
|
| 259 |
+
of semantically well-defined feature. Syntactic transformation. Seldom.
|
| 260 |
+
|
| 261 |
+
**Change:** C allows directly comparing two objects of array type; C++
|
| 262 |
+
does not. **Rationale:** The behavior is confusing because it compares
|
| 263 |
+
not the contents of the two arrays, but their addresses. **Effect on
|
| 264 |
+
original feature:** Deletion of semantically well-defined feature that
|
| 265 |
+
had unspecified behavior in common use cases. Violations will be
|
| 266 |
+
diagnosed by the C++ translator. The original behavior can be replicated
|
| 267 |
+
by explicitly casting either array to a pointer, such as by using a
|
| 268 |
+
unary `+`.
|
| 269 |
+
|
| 270 |
+
[*Example 6*:
|
| 271 |
+
|
| 272 |
+
``` cpp
|
| 273 |
+
int arr1[5];
|
| 274 |
+
int arr2[5];
|
| 275 |
+
int same = arr1 == arr2; // valid C, ill-formed C++
|
| 276 |
+
int idem = arr1 == +arr2; // valid in both C and C++
|
| 277 |
+
```
|
| 278 |
+
|
| 279 |
+
— *end example*]
|
| 280 |
+
|
| 281 |
+
Rare.
|
| 282 |
|
| 283 |
**Change:** The result of a conditional expression, an assignment
|
| 284 |
expression, or a comma expression may be an lvalue. **Rationale:** C++
|
| 285 |
is an object-oriented language, placing relatively more emphasis on
|
| 286 |
lvalues. For example, function calls may yield lvalues. **Effect on
|
| 287 |
original feature:** Change to semantics of well-defined feature. Some C
|
| 288 |
expressions that implicitly rely on lvalue-to-rvalue conversions will
|
| 289 |
+
yield different results.
|
| 290 |
+
|
| 291 |
+
[*Example 7*:
|
| 292 |
|
| 293 |
``` cpp
|
| 294 |
char arr[100];
|
| 295 |
sizeof(0, arr)
|
| 296 |
```
|
| 297 |
|
| 298 |
+
yields `100` in C++ and `sizeof(char*)` in C.
|
|
|
|
| 299 |
|
| 300 |
+
— *end example*]
|
| 301 |
+
|
| 302 |
+
Programs must add explicit casts to the appropriate rvalue. Rare.
|
| 303 |
+
|
| 304 |
+
### [[stmt]]: statements <a id="diff.stat">[[diff.stat]]</a>
|
| 305 |
|
| 306 |
**Change:** It is now invalid to jump past a declaration with explicit
|
| 307 |
or implicit initializer (except across entire block not entered).
|
| 308 |
**Rationale:** Constructors used in initializers may allocate resources
|
| 309 |
which need to be de-allocated upon leaving the block. Allowing jump past
|
|
|
|
| 327 |
Deletion of semantically well-defined feature. Semantic transformation.
|
| 328 |
Add an appropriate return value to the source code, such as zero.
|
| 329 |
Seldom. For several years, many existing C implementations have produced
|
| 330 |
warnings in this case.
|
| 331 |
|
| 332 |
+
### [[dcl]]: declarations <a id="diff.dcl">[[diff.dcl]]</a>
|
| 333 |
|
| 334 |
**Change:** In C++, the `static` or `extern` specifiers can only be
|
| 335 |
applied to names of objects or functions.
|
| 336 |
Using these specifiers with type declarations is illegal in C++. In C,
|
| 337 |
these specifiers are ignored when used on type declarations.
|
| 338 |
|
| 339 |
+
[*Example 1*:
|
| 340 |
|
| 341 |
``` cpp
|
| 342 |
static struct S { // valid C, invalid in C++{}
|
| 343 |
int i;
|
| 344 |
};
|
| 345 |
```
|
| 346 |
|
| 347 |
+
— *end example*]
|
| 348 |
+
|
| 349 |
**Rationale:** Storage class specifiers don’t have any meaning when
|
| 350 |
associated with a type. In C++, class members can be declared with the
|
| 351 |
`static` storage class specifier. Storage class specifiers on type
|
| 352 |
declarations can be confusing for users. **Effect on original feature:**
|
| 353 |
Deletion of semantically well-defined feature. Syntactic transformation.
|
|
|
|
| 362 |
name declared in the same scope (except if the typedef is a synonym of
|
| 363 |
the class name with the same name). In C, a *typedef-name* and a struct
|
| 364 |
tag name declared in the same scope can have the same name (because they
|
| 365 |
have different name spaces).
|
| 366 |
|
| 367 |
+
[*Example 2*:
|
| 368 |
|
| 369 |
``` cpp
|
| 370 |
typedef struct name1 { ... } name1; // valid C and C++{}
|
| 371 |
struct name { ... };
|
| 372 |
typedef int name; // valid C, invalid C++{}
|
| 373 |
```
|
| 374 |
|
| 375 |
+
— *end example*]
|
| 376 |
+
|
| 377 |
**Rationale:** For ease of use, C++ doesn’t require that a type name be
|
| 378 |
prefixed with the keywords `class`, `struct` or `union` when used in
|
| 379 |
object declarations or type casts.
|
| 380 |
|
| 381 |
+
[*Example 3*:
|
| 382 |
|
| 383 |
``` cpp
|
| 384 |
class name { ... };
|
| 385 |
name i; // i has type class name
|
| 386 |
```
|
| 387 |
|
| 388 |
+
— *end example*]
|
| 389 |
+
|
| 390 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 391 |
feature. Semantic transformation. One of the 2 types has to be renamed.
|
| 392 |
Seldom.
|
| 393 |
|
| 394 |
\[see also [[basic.link]]\] **Change:** Const objects must be
|
|
|
|
| 398 |
well-defined feature. Semantic transformation. Seldom.
|
| 399 |
|
| 400 |
**Change:** The keyword `auto` cannot be used as a storage class
|
| 401 |
specifier.
|
| 402 |
|
| 403 |
+
[*Example 4*:
|
| 404 |
|
| 405 |
``` cpp
|
| 406 |
void f() {
|
| 407 |
auto int x; // valid C, invalid C++{}
|
| 408 |
}
|
| 409 |
```
|
| 410 |
|
| 411 |
+
— *end example*]
|
| 412 |
+
|
| 413 |
**Rationale:** Allowing the use of `auto` to deduce the type of a
|
| 414 |
variable from its initializer results in undesired interpretations of
|
| 415 |
`auto` as a storage class specifier in certain contexts. **Effect on
|
| 416 |
original feature:** Deletion of semantically well-defined feature.
|
| 417 |
Syntactic transformation. Rare.
|
| 418 |
|
| 419 |
**Change:** In C++, a function declared with an empty parameter list
|
| 420 |
takes no arguments. In C, an empty parameter list means that the number
|
| 421 |
and type of the function arguments are unknown.
|
| 422 |
|
| 423 |
+
[*Example 5*:
|
| 424 |
|
| 425 |
``` cpp
|
| 426 |
int f(); // means int f(void) in C++{}
|
| 427 |
// int f( unknown ) in C
|
| 428 |
```
|
| 429 |
|
| 430 |
+
— *end example*]
|
| 431 |
+
|
| 432 |
+
**Rationale:** This is to avoid function calls with the wrong number or
|
| 433 |
+
type of arguments. **Effect on original feature:** Change to semantics
|
| 434 |
+
of well-defined feature. This feature was marked as “obsolescent” in C.
|
| 435 |
+
Syntactic transformation. The function declarations using C incomplete
|
| 436 |
+
declaration style must be completed to become full prototype
|
| 437 |
+
declarations. A program may need to be updated further if different
|
| 438 |
+
calls to the same (non-prototype) function have different numbers of
|
| 439 |
+
arguments or if the type of corresponding arguments differed. Common.
|
| 440 |
|
| 441 |
\[see [[expr.sizeof]]\] **Change:** In C++, types may not be defined in
|
| 442 |
return or parameter types. In C, these type definitions are allowed.
|
| 443 |
|
| 444 |
+
[*Example 6*:
|
| 445 |
|
| 446 |
``` cpp
|
| 447 |
void f( struct S { int a; } arg ) {} // valid C, invalid C++{}
|
| 448 |
enum E { A, B, C } f() {} // valid C, invalid C++{}
|
| 449 |
```
|
| 450 |
|
| 451 |
+
— *end example*]
|
| 452 |
+
|
| 453 |
**Rationale:** When comparing types in different translation units, C++
|
| 454 |
relies on name equivalence when C relies on structural equivalence.
|
| 455 |
Regarding parameter types: since the type defined in a parameter list
|
| 456 |
would be in the scope of the function, the only legal calls in C++ would
|
| 457 |
be from within the function itself. **Effect on original feature:**
|
|
|
|
| 471 |
for non-static data members must be specified in declaration order,
|
| 472 |
designators for array elements and nested designators are not supported,
|
| 473 |
and designated and non-designated initializers cannot be mixed in the
|
| 474 |
same initializer list.
|
| 475 |
|
| 476 |
+
[*Example 7*:
|
| 477 |
|
| 478 |
``` cpp
|
| 479 |
struct A { int x, y; };
|
| 480 |
struct B { struct A a; };
|
| 481 |
struct A a = {.y = 1, .x = 2}; // valid C, invalid C++{}
|
| 482 |
int arr[3] = {[1] = 5}; // valid C, invalid C++{}
|
| 483 |
struct B b = {.a.x = 0}; // valid C, invalid C++{}
|
| 484 |
struct A c = {.x = 1, 2}; // valid C, invalid C++{}
|
| 485 |
```
|
| 486 |
|
| 487 |
+
— *end example*]
|
| 488 |
+
|
| 489 |
**Rationale:** In C++, members are destroyed in reverse construction
|
| 490 |
order and the elements of an initializer list are evaluated in lexical
|
| 491 |
+
order, so member initializers must be specified in order. Array
|
| 492 |
designators conflict with *lambda-expression* syntax. Nested designators
|
| 493 |
are seldom used. **Effect on original feature:** Deletion of feature
|
| 494 |
that is incompatible with C++. Syntactic transformation. Out-of-order
|
| 495 |
initializers are common. The other features are seldom used.
|
| 496 |
|
|
|
|
| 498 |
string, the number of characters in the string (including the
|
| 499 |
terminating `'\0'`) must not exceed the number of elements in the array.
|
| 500 |
In C, an array can be initialized with a string even if the array is not
|
| 501 |
large enough to contain the string-terminating `'\0'`.
|
| 502 |
|
| 503 |
+
[*Example 8*:
|
| 504 |
|
| 505 |
``` cpp
|
| 506 |
char array[4] = "abcd"; // valid C, invalid C++{}
|
| 507 |
```
|
| 508 |
|
| 509 |
+
— *end example*]
|
| 510 |
+
|
| 511 |
**Rationale:** When these non-terminated arrays are manipulated by
|
| 512 |
standard string functions, there is potential for major catastrophe.
|
| 513 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 514 |
feature. Semantic transformation. The arrays must be declared one
|
| 515 |
element bigger to contain the string terminating `'\0'`. Seldom. This
|
|
|
|
| 517 |
|
| 518 |
**Change:** C++ objects of enumeration type can only be assigned values
|
| 519 |
of the same enumeration type. In C, objects of enumeration type can be
|
| 520 |
assigned values of any integral type.
|
| 521 |
|
| 522 |
+
[*Example 9*:
|
| 523 |
|
| 524 |
``` cpp
|
| 525 |
enum color { red, blue, green };
|
| 526 |
enum color c = 1; // valid C, invalid C++{}
|
| 527 |
```
|
| 528 |
|
| 529 |
+
— *end example*]
|
| 530 |
+
|
| 531 |
**Rationale:** The type-safe nature of C++. **Effect on original
|
| 532 |
feature:** Deletion of semantically well-defined feature. Syntactic
|
| 533 |
transformation. (The type error produced by the assignment can be
|
| 534 |
automatically corrected by applying an explicit cast.) Common.
|
| 535 |
|
| 536 |
**Change:** In C++, the type of an enumerator is its enumeration. In C,
|
| 537 |
the type of an enumerator is `int`.
|
| 538 |
|
| 539 |
+
[*Example 10*:
|
| 540 |
|
| 541 |
``` cpp
|
| 542 |
enum e { A };
|
| 543 |
sizeof(A) == sizeof(int) // in C
|
| 544 |
sizeof(A) == sizeof(e) // in C++{}
|
| 545 |
/* and sizeof(int) is not necessarily equal to sizeof(e) */
|
| 546 |
```
|
| 547 |
|
| 548 |
+
— *end example*]
|
| 549 |
+
|
| 550 |
**Rationale:** In C++, an enumeration is a distinct type. **Effect on
|
| 551 |
original feature:** Change to semantics of well-defined feature.
|
| 552 |
Semantic transformation. Seldom. The only time this affects existing C
|
| 553 |
code is when the size of an enumerator is taken. Taking the size of an
|
| 554 |
enumerator is not a common C coding practice.
|
| 555 |
|
| 556 |
**Change:** In C++, an *alignment-specifier* is an
|
| 557 |
*attribute-specifier*. In C, an *alignment-specifier* is a .
|
| 558 |
|
| 559 |
+
[*Example 11*:
|
| 560 |
|
| 561 |
``` cpp
|
| 562 |
#include <stdalign.h>
|
| 563 |
unsigned alignas(8) int x; // valid C, invalid C++{}
|
| 564 |
unsigned int y alignas(8); // valid C++{}, invalid C
|
| 565 |
```
|
| 566 |
|
| 567 |
+
— *end example*]
|
| 568 |
+
|
| 569 |
**Rationale:** C++ requires unambiguous placement of the
|
| 570 |
*alignment-specifier*. **Effect on original feature:** Deletion of
|
| 571 |
semantically well-defined feature. Syntactic transformation. Seldom.
|
| 572 |
|
| 573 |
### [[class]]: classes <a id="diff.class">[[diff.class]]</a>
|
|
|
|
| 576 |
introduces the class name into the scope where it is declared and hides
|
| 577 |
any object, function or other declaration of that name in an enclosing
|
| 578 |
scope. In C, an inner scope declaration of a struct tag name never hides
|
| 579 |
the name of an object or function in an outer scope.
|
| 580 |
|
| 581 |
+
[*Example 1*:
|
| 582 |
|
| 583 |
``` cpp
|
| 584 |
int x[99];
|
| 585 |
void f() {
|
| 586 |
struct x { int a; };
|
| 587 |
sizeof(x); /* size of the array in C */
|
| 588 |
/* size of the struct in \textit{\textrm{C++{}}} */
|
| 589 |
}
|
| 590 |
```
|
| 591 |
|
| 592 |
+
— *end example*]
|
| 593 |
+
|
| 594 |
**Rationale:** This is one of the few incompatibilities between C and
|
| 595 |
C++ that can be attributed to the new C++ name space definition where a
|
| 596 |
name can be declared as a type and as a non-type in a single scope
|
| 597 |
causing the non-type name to hide the type name and requiring that the
|
| 598 |
keywords `class`, `struct`, `union` or `enum` be used to refer to the
|
|
|
|
| 608 |
Seldom.
|
| 609 |
|
| 610 |
**Change:** Copying volatile objects.
|
| 611 |
|
| 612 |
The implicitly-declared copy constructor and implicitly-declared copy
|
| 613 |
+
assignment operator cannot make a copy of a volatile lvalue.
|
| 614 |
+
|
| 615 |
+
[*Example 2*:
|
| 616 |
+
|
| 617 |
+
The following is valid in C:
|
| 618 |
|
| 619 |
``` cpp
|
| 620 |
struct X { int i; };
|
| 621 |
volatile struct X x1 = {0};
|
| 622 |
struct X x2 = x1; // invalid C++{}
|
| 623 |
struct X x3;
|
| 624 |
x3 = x1; // also invalid C++{}
|
| 625 |
```
|
| 626 |
|
| 627 |
+
— *end example*]
|
| 628 |
+
|
| 629 |
**Rationale:** Several alternatives were debated at length. Changing the
|
| 630 |
parameter to `volatile` `const` `X&` would greatly complicate the
|
| 631 |
generation of efficient code for class objects. Discussion of providing
|
| 632 |
two alternative signatures for these implicitly-defined operations
|
| 633 |
raised unanswered concerns about creating ambiguities and complicating
|
|
|
|
| 647 |
|
| 648 |
**Change:** In C++, the name of a nested class is local to its enclosing
|
| 649 |
class. In C the name of the nested class belongs to the same scope as
|
| 650 |
the name of the outermost enclosing class.
|
| 651 |
|
| 652 |
+
[*Example 3*:
|
| 653 |
|
| 654 |
``` cpp
|
| 655 |
struct X {
|
| 656 |
struct Y { ... } y;
|
| 657 |
};
|
| 658 |
struct Y yy; // valid C, invalid C++{}
|
| 659 |
```
|
| 660 |
|
| 661 |
+
— *end example*]
|
| 662 |
+
|
| 663 |
**Rationale:** C++ classes have member functions which require that
|
| 664 |
classes establish scopes. The C rule would leave classes as an
|
| 665 |
incomplete scope mechanism which would prevent C++ programmers from
|
| 666 |
maintaining locality within a class. A coherent set of scope rules for
|
| 667 |
C++ based on the C rule would be very complicated and C++ programmers
|
| 668 |
would be unable to predict reliably the meanings of nontrivial examples
|
| 669 |
involving nested or local functions. **Effect on original feature:**
|
| 670 |
Change to semantics of well-defined feature. Semantic transformation. To
|
| 671 |
make the struct type name visible in the scope of the enclosing struct,
|
| 672 |
the struct tag can be declared in the scope of the enclosing struct,
|
| 673 |
+
before the enclosing struct is defined.
|
| 674 |
+
|
| 675 |
+
[*Example 4*:
|
| 676 |
|
| 677 |
``` cpp
|
| 678 |
struct Y; // struct Y and struct X are at the same scope
|
| 679 |
struct X {
|
| 680 |
struct Y { ... } y;
|
| 681 |
};
|
| 682 |
```
|
| 683 |
|
| 684 |
+
— *end example*]
|
| 685 |
+
|
| 686 |
All the definitions of C struct types enclosed in other struct
|
| 687 |
definitions and accessed outside the scope of the enclosing struct can
|
| 688 |
be exported to the scope of the enclosing struct. Note: this is a
|
| 689 |
consequence of the difference in scope rules, which is documented in
|
| 690 |
[[basic.scope]]. Seldom.
|
| 691 |
|
| 692 |
**Change:** In C++, a *typedef-name* may not be redeclared in a class
|
| 693 |
definition after being used in that definition.
|
| 694 |
|
| 695 |
+
[*Example 5*:
|
| 696 |
|
| 697 |
``` cpp
|
| 698 |
typedef int I;
|
| 699 |
struct S {
|
| 700 |
I i;
|
| 701 |
int I; // valid C, invalid C++{}
|
| 702 |
};
|
| 703 |
```
|
| 704 |
|
| 705 |
+
— *end example*]
|
| 706 |
+
|
| 707 |
**Rationale:** When classes become complicated, allowing such a
|
| 708 |
redefinition after the type has been used can create confusion for C++
|
| 709 |
programmers as to what the meaning of `I` really is. **Effect on
|
| 710 |
original feature:** Deletion of semantically well-defined feature.
|
| 711 |
Semantic transformation. Either the type or the struct member has to be
|
| 712 |
renamed. Seldom.
|
| 713 |
|
| 714 |
### [[cpp]]: preprocessing directives <a id="diff.cpp">[[diff.cpp]]</a>
|
| 715 |
|
| 716 |
**Change:** Whether `__STDC__` is defined and if so, what its value is,
|
| 717 |
+
are *implementation-defined*. **Rationale:** C++ is not identical to C.
|
| 718 |
+
Mandating that `__STDC__` be defined would require that translators make
|
| 719 |
+
an incorrect claim. **Effect on original feature:** Change to semantics
|
| 720 |
+
of well-defined feature. Semantic transformation. Programs and headers
|
| 721 |
+
that reference `__STDC__` are quite common.
|
| 722 |
|