- tmp/tmpkagetu3w/{from.md → to.md} +258 -247
tmp/tmpkagetu3w/{from.md → to.md}
RENAMED
|
@@ -1,24 +1,24 @@
|
|
| 1 |
## C++ and ISO C <a id="diff.iso">[[diff.iso]]</a>
|
| 2 |
|
| 3 |
-
This subclause lists the differences between C++and ISO C,
|
| 4 |
-
chapters of this document.
|
| 5 |
|
| 6 |
-
###
|
| 7 |
|
| 8 |
-
|
| 9 |
New keywords are added to C++; see [[lex.key]]. **Rationale:** These
|
| 10 |
keywords were added in order to implement the new semantics of C++.
|
| 11 |
**Effect on original feature:** Change to semantics of well-defined
|
| 12 |
feature. Any ISO C programs that used any of these keywords as
|
| 13 |
identifiers are not valid C++ programs. Syntactic transformation.
|
| 14 |
Converting one specific program is easy. Converting a large collection
|
| 15 |
of related programs takes more work. Common.
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
``` cpp
|
| 22 |
int function( int i );
|
| 23 |
int function( char c );
|
| 24 |
|
|
@@ -34,49 +34,49 @@ sizeof('x') == sizeof(int)
|
|
| 34 |
```
|
| 35 |
|
| 36 |
will not work the same as C++ programs. Simple. Programs which depend
|
| 37 |
upon `sizeof('x')` are probably rare.
|
| 38 |
|
| 39 |
-
|
| 40 |
-
The type of a string
|
| 41 |
-
of `const char`”. The type of a
|
| 42 |
-
from “array of
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
`const
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
``` cpp
|
| 53 |
-
char* p = "abc"; // valid in C, invalid in C++
|
| 54 |
void f(char*) {
|
| 55 |
char* p = (char*)"abc"; // OK: cast added
|
| 56 |
f(p);
|
| 57 |
f((char*)"def"); // OK: cast added
|
| 58 |
}
|
| 59 |
```
|
| 60 |
|
| 61 |
-
Programs that have a legitimate reason to treat string
|
| 62 |
-
|
| 63 |
|
| 64 |
-
###
|
| 65 |
|
| 66 |
-
|
| 67 |
-
C.
|
| 68 |
E.g., at file scope,
|
| 69 |
|
| 70 |
``` cpp
|
| 71 |
int i;
|
| 72 |
int i;
|
| 73 |
```
|
| 74 |
|
| 75 |
is valid in C, invalid in C++. This makes it impossible to define
|
| 76 |
-
mutually referential file-local static
|
| 77 |
-
restricted to the syntactic forms of C. For example,
|
| 78 |
|
| 79 |
``` cpp
|
| 80 |
struct X { int i; struct X* next; };
|
| 81 |
|
| 82 |
static struct X a;
|
|
@@ -86,94 +86,89 @@ static struct X a = { 1, &b };
|
|
| 86 |
|
| 87 |
**Rationale:** This avoids having different initialization rules for
|
| 88 |
fundamental types and user-defined types. **Effect on original
|
| 89 |
feature:** Deletion of semantically well-defined feature. Semantic
|
| 90 |
transformation. In C++, the initializer for one of a set of
|
| 91 |
-
mutually-referential file-local
|
| 92 |
-
call to achieve the initialization. Seldom.
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
The latter is probably rare.
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
explicit initializer for each
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
For example, otherwise-identical `struct` types with different tag names
|
| 121 |
are “compatible” in C but are distinctly different types in C++.
|
| 122 |
**Rationale:** Stricter type checking is essential for C++. **Effect on
|
| 123 |
original feature:** Deletion of semantically well-defined feature.
|
| 124 |
Semantic transformation. The “typesafe linkage” mechanism will find
|
| 125 |
many, but not all, of such problems. Those problems not found by
|
| 126 |
typesafe linkage will continue to function properly, according to the
|
| 127 |
-
“layout compatibility rules” of this
|
| 128 |
|
| 129 |
-
###
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
|
| 134 |
``` cpp
|
| 135 |
char a[10];
|
| 136 |
void* b=a;
|
| 137 |
void foo() {
|
| 138 |
char* c=b;
|
| 139 |
}
|
| 140 |
```
|
| 141 |
|
| 142 |
ISO C will accept this usage of pointer to void being assigned to a
|
| 143 |
-
pointer to object type. C++will not. **Rationale:** C++tries harder
|
| 144 |
-
C to enforce compile-time type safety. **Effect on original
|
| 145 |
-
Deletion of semantically well-defined feature. Could be
|
| 146 |
-
Violations will be diagnosed by the C++translator. The fix
|
| 147 |
-
cast. For example:
|
| 148 |
|
| 149 |
``` cpp
|
| 150 |
char* c = (char*) b;
|
| 151 |
```
|
| 152 |
|
| 153 |
This is fairly widely used but it is good programming practice to add
|
| 154 |
the cast when assigning pointer-to-void to pointer-to-object. Some ISO C
|
| 155 |
translators will give a warning if the cast is not used.
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
original feature:** Deletion of semantically well-defined feature. Note:
|
| 162 |
-
the original feature was labeled as “obsolescent” in ISO C. Syntactic
|
| 163 |
transformation. Facilities for producing explicit function declarations
|
| 164 |
are fairly widespread commercially. Common.
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
Standard.
|
| 172 |
|
| 173 |
-
|
| 174 |
-
declarations, not in expressions.
|
| 175 |
In C, a sizeof expression or cast expression may define a new type. For
|
| 176 |
example,
|
| 177 |
|
| 178 |
``` cpp
|
| 179 |
p = (void*)(struct x {int i;} *)0;
|
|
@@ -182,44 +177,43 @@ p = (void*)(struct x {int i;} *)0;
|
|
| 182 |
defines a new type, struct `x`. **Rationale:** This prohibition helps to
|
| 183 |
clarify the location of definitions in the source code. **Effect on
|
| 184 |
original feature:** Deletion of semantically well-defined feature.
|
| 185 |
Syntactic transformation. Seldom.
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
|
| 195 |
``` cpp
|
| 196 |
char arr[100];
|
| 197 |
sizeof(0, arr)
|
| 198 |
```
|
| 199 |
|
| 200 |
yields `100` in C++ and `sizeof(char*)` in C. Programs must add explicit
|
| 201 |
casts to the appropriate rvalue. Rare.
|
| 202 |
|
| 203 |
-
###
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
transformation. Seldom.
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
objects. If some flow paths execute a return without specifying any
|
| 222 |
value, the implementation must embody many more complications. Besides,
|
| 223 |
promising to return a value of a given type, and then not returning such
|
| 224 |
a value, has always been recognized to be a questionable practice,
|
| 225 |
tolerated only because very-old C had no distinction between void
|
|
@@ -227,21 +221,21 @@ functions and int functions. **Effect on original feature:** Deletion of
|
|
| 227 |
semantically well-defined feature. Semantic transformation. Add an
|
| 228 |
appropriate return value to the source code, such as zero. Seldom. For
|
| 229 |
several years, many existing C implementations have produced warnings in
|
| 230 |
this case.
|
| 231 |
|
| 232 |
-
###
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
Using these specifiers with type declarations is illegal in C++. In C,
|
| 237 |
these specifiers are ignored when used on type declarations.
|
| 238 |
|
| 239 |
Example:
|
| 240 |
|
| 241 |
``` cpp
|
| 242 |
-
static struct S { // valid C, invalid in C++
|
| 243 |
int i;
|
| 244 |
};
|
| 245 |
```
|
| 246 |
|
| 247 |
**Rationale:** Storage class specifiers don’t have any meaning when
|
|
@@ -249,27 +243,27 @@ associated with a type. In C++, class members can be declared with the
|
|
| 249 |
`static` storage class specifier. Allowing storage class specifiers on
|
| 250 |
type declarations could render the code confusing for users. **Effect on
|
| 251 |
original feature:** Deletion of semantically well-defined feature.
|
| 252 |
Syntactic transformation. Seldom.
|
| 253 |
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
|
| 265 |
Example:
|
| 266 |
|
| 267 |
``` cpp
|
| 268 |
-
typedef struct name1 { ... } name1; // valid C and C++
|
| 269 |
struct name { ... };
|
| 270 |
-
typedef int name; // valid C, invalid C++
|
| 271 |
```
|
| 272 |
|
| 273 |
**Rationale:** For ease of use, C++ doesn’t require that a type name be
|
| 274 |
prefixed with the keywords `class`, `struct` or `union` when used in
|
| 275 |
object declarations or type casts.
|
|
@@ -283,18 +277,17 @@ name i; // i has type class name
|
|
| 283 |
|
| 284 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 285 |
feature. Semantic transformation. One of the 2 types has to be renamed.
|
| 286 |
Seldom.
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
Seldom.
|
| 294 |
|
| 295 |
-
|
| 296 |
|
| 297 |
In C++ a *decl-specifier-seq* must contain a *type-specifier*, unless it
|
| 298 |
is followed by a declarator for a constructor, a destructor, or a
|
| 299 |
conversion function. In the following example, the left-hand column
|
| 300 |
presents valid C; the right-hand column presents equivalent C++:
|
|
@@ -312,69 +305,33 @@ declarations. Explicit declaration is increasingly considered to be
|
|
| 312 |
proper style. Liaison with WG14 (C) indicated support for (at least)
|
| 313 |
deprecating implicit int in the next revision of C. **Effect on original
|
| 314 |
feature:** Deletion of semantically well-defined feature. Syntactic
|
| 315 |
transformation. Could be automated. Common.
|
| 316 |
|
| 317 |
-
|
| 318 |
-
|
| 319 |
|
| 320 |
``` cpp
|
| 321 |
void f() {
|
| 322 |
-
auto int x;
|
| 323 |
}
|
| 324 |
```
|
| 325 |
|
| 326 |
**Rationale:** Allowing the use of `auto` to deduce the type of a
|
| 327 |
variable from its initializer results in undesired interpretations of
|
| 328 |
`auto` as a storage class specifier in certain contexts. **Effect on
|
| 329 |
original feature:** Deletion of semantically well-defined feature.
|
| 330 |
Syntactic transformation. Rare.
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
|
| 336 |
Example:
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
-
|
| 340 |
-
enum color c = 1; // valid C, invalid C++
|
| 341 |
-
```
|
| 342 |
-
|
| 343 |
-
**Rationale:** The type-safe nature of C++. **Effect on original
|
| 344 |
-
feature:** Deletion of semantically well-defined feature. Syntactic
|
| 345 |
-
transformation. (The type error produced by the assignment can be
|
| 346 |
-
automatically corrected by applying an explicit cast.) Common.
|
| 347 |
-
|
| 348 |
-
[[dcl.enum]] **Change:** In C++, the type of an enumerator is its
|
| 349 |
-
enumeration. In C, the type of an enumerator is `int`.
|
| 350 |
-
|
| 351 |
-
Example:
|
| 352 |
-
|
| 353 |
-
``` cpp
|
| 354 |
-
enum e { A };
|
| 355 |
-
sizeof(A) == sizeof(int) // in C
|
| 356 |
-
sizeof(A) == sizeof(e) // in C++
|
| 357 |
-
/* and sizeof(int) is not necessarily equal to sizeof(e) */
|
| 358 |
-
```
|
| 359 |
-
|
| 360 |
-
**Rationale:** In C++, an enumeration is a distinct type. **Effect on
|
| 361 |
-
original feature:** Change to semantics of well-defined feature.
|
| 362 |
-
Semantic transformation. Seldom. The only time this affects existing C
|
| 363 |
-
code is when the size of an enumerator is taken. Taking the size of an
|
| 364 |
-
enumerator is not a common C coding practice.
|
| 365 |
-
|
| 366 |
-
### Clause [[dcl.decl]]: declarators <a id="diff.decl">[[diff.decl]]</a>
|
| 367 |
-
|
| 368 |
-
[[dcl.fct]] **Change:** In C++, a function declared with an empty
|
| 369 |
-
parameter list takes no arguments. In C, an empty parameter list means
|
| 370 |
-
that the number and type of the function arguments are unknown.
|
| 371 |
-
|
| 372 |
-
Example:
|
| 373 |
-
|
| 374 |
-
``` cpp
|
| 375 |
-
int f(); // means int f(void) in C++
|
| 376 |
// int f( unknown ) in C
|
| 377 |
```
|
| 378 |
|
| 379 |
**Rationale:** This is to avoid erroneous function calls (i.e., function
|
| 380 |
calls with the wrong number or type of arguments). **Effect on original
|
|
@@ -384,74 +341,131 @@ declarations using C incomplete declaration style must be completed to
|
|
| 384 |
become full prototype declarations. A program may need to be updated
|
| 385 |
further if different calls to the same (non-prototype) function have
|
| 386 |
different numbers of arguments or if the type of corresponding arguments
|
| 387 |
differed. Common.
|
| 388 |
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
allowed.
|
| 392 |
|
| 393 |
Example:
|
| 394 |
|
| 395 |
``` cpp
|
| 396 |
-
void f( struct S { int a; } arg ) {} // valid C, invalid C++
|
| 397 |
-
enum E { A, B, C } f() {} // valid C, invalid C++
|
| 398 |
```
|
| 399 |
|
| 400 |
-
**Rationale:** When comparing types in different translation units,
|
| 401 |
-
|
| 402 |
Regarding parameter types: since the type defined in a parameter list
|
| 403 |
would be in the scope of the function, the only legal calls in C++ would
|
| 404 |
be from within the function itself. **Effect on original feature:**
|
| 405 |
Deletion of semantically well-defined feature. Semantic transformation.
|
| 406 |
The type definitions must be moved to file scope, or in header files.
|
| 407 |
Seldom. This style of type definition is seen as poor coding style.
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
|
| 423 |
Example:
|
| 424 |
|
| 425 |
``` cpp
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
```
|
| 428 |
|
| 429 |
**Rationale:** When these non-terminated arrays are manipulated by
|
| 430 |
standard string functions, there is potential for major catastrophe.
|
| 431 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 432 |
feature. Semantic transformation. The arrays must be declared one
|
| 433 |
element bigger to contain the string terminating `'\0'`. Seldom. This
|
| 434 |
style of array initialization is seen as poor coding style.
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
|
| 445 |
Example:
|
| 446 |
|
| 447 |
``` cpp
|
| 448 |
int x[99];
|
| 449 |
void f() {
|
| 450 |
struct x { int a; };
|
| 451 |
sizeof(x); /* size of the array in C */
|
| 452 |
-
/* size of the struct in C++ */
|
| 453 |
}
|
| 454 |
```
|
| 455 |
|
| 456 |
**Rationale:** This is one of the few incompatibilities between C and
|
| 457 |
C++ that can be attributed to the new C++ name space definition where a
|
|
@@ -463,33 +477,58 @@ conveniences to C++programmers and helps making the use of the
|
|
| 463 |
user-defined types as similar as possible to the use of fundamental
|
| 464 |
types. The advantages of the new name space definition were judged to
|
| 465 |
outweigh by far the incompatibility with C described above. **Effect on
|
| 466 |
original feature:** Change to semantics of well-defined feature.
|
| 467 |
Semantic transformation. If the hidden name that needs to be accessed is
|
| 468 |
-
at global scope, the `::` C++operator can be used. If the hidden name
|
| 469 |
-
at block scope, either the type or the struct tag has to be renamed.
|
| 470 |
Seldom.
|
| 471 |
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
|
| 484 |
Example:
|
| 485 |
|
| 486 |
``` cpp
|
| 487 |
struct X {
|
| 488 |
struct Y { ... } y;
|
| 489 |
};
|
| 490 |
-
struct Y yy; // valid C, invalid C++
|
| 491 |
```
|
| 492 |
|
| 493 |
**Rationale:** C++ classes have member functions which require that
|
| 494 |
classes establish scopes. The C rule would leave classes as an
|
| 495 |
incomplete scope mechanism which would prevent C++ programmers from
|
|
@@ -513,64 +552,36 @@ All the definitions of C struct types enclosed in other struct
|
|
| 513 |
definitions and accessed outside the scope of the enclosing struct could
|
| 514 |
be exported to the scope of the enclosing struct. Note: this is a
|
| 515 |
consequence of the difference in scope rules, which is documented in
|
| 516 |
[[basic.scope]]. Seldom.
|
| 517 |
|
| 518 |
-
|
| 519 |
-
|
| 520 |
|
| 521 |
Example:
|
| 522 |
|
| 523 |
``` cpp
|
| 524 |
typedef int I;
|
| 525 |
struct S {
|
| 526 |
I i;
|
| 527 |
-
int I;
|
| 528 |
};
|
| 529 |
```
|
| 530 |
|
| 531 |
**Rationale:** When classes become complicated, allowing such a
|
| 532 |
redefinition after the type has been used can create confusion for C++
|
| 533 |
programmers as to what the meaning of `I` really is. **Effect on
|
| 534 |
original feature:** Deletion of semantically well-defined feature.
|
| 535 |
Semantic transformation. Either the type or the struct member has to be
|
| 536 |
renamed. Seldom.
|
| 537 |
|
| 538 |
-
###
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
``
|
| 547 |
-
|
| 548 |
-
volatile struct X x1 = {0};
|
| 549 |
-
struct X x2 = x1; // invalid C++
|
| 550 |
-
struct X x3;
|
| 551 |
-
x3 = x1; // also invalid C++
|
| 552 |
-
```
|
| 553 |
-
|
| 554 |
-
**Rationale:** Several alternatives were debated at length. Changing the
|
| 555 |
-
parameter to `volatile` `const` `X&` would greatly complicate the
|
| 556 |
-
generation of efficient code for class objects. Discussion of providing
|
| 557 |
-
two alternative signatures for these implicitly-defined operations
|
| 558 |
-
raised unanswered concerns about creating ambiguities and complicating
|
| 559 |
-
the rules that specify the formation of these operators according to the
|
| 560 |
-
bases and members. **Effect on original feature:** Deletion of
|
| 561 |
-
semantically well-defined feature. Semantic transformation. If volatile
|
| 562 |
-
semantics are required for the copy, a user-declared constructor or
|
| 563 |
-
assignment must be provided. If non-volatile semantics are required, an
|
| 564 |
-
explicit `const_cast` can be used. Seldom.
|
| 565 |
-
|
| 566 |
-
### Clause [[cpp]]: preprocessing directives <a id="diff.cpp">[[diff.cpp]]</a>
|
| 567 |
-
|
| 568 |
-
[[cpp.predefined]] **Change:** Whether `__STDC__` is defined and if so,
|
| 569 |
-
what its value is, are *implementation-defined*. **Rationale:** C++is
|
| 570 |
-
not identical to ISO C. Mandating that `__STDC__` be defined would
|
| 571 |
-
require that translators make an incorrect claim. Each implementation
|
| 572 |
-
must choose the behavior that will be most useful to its marketplace.
|
| 573 |
-
**Effect on original feature:** Change to semantics of well-defined
|
| 574 |
-
feature. Semantic transformation. Programs and headers that reference
|
| 575 |
-
`__STDC__` are quite common.
|
| 576 |
|
|
|
|
| 1 |
## C++ and ISO C <a id="diff.iso">[[diff.iso]]</a>
|
| 2 |
|
| 3 |
+
This subclause lists the differences between C++ and ISO C, in addition
|
| 4 |
+
to those listed above, by the chapters of this document.
|
| 5 |
|
| 6 |
+
### [[lex]]: lexical conventions <a id="diff.lex">[[diff.lex]]</a>
|
| 7 |
|
| 8 |
+
**Change:** New Keywords
|
| 9 |
New keywords are added to C++; see [[lex.key]]. **Rationale:** These
|
| 10 |
keywords were added in order to implement the new semantics of C++.
|
| 11 |
**Effect on original feature:** Change to semantics of well-defined
|
| 12 |
feature. Any ISO C programs that used any of these keywords as
|
| 13 |
identifiers are not valid C++ programs. Syntactic transformation.
|
| 14 |
Converting one specific program is easy. Converting a large collection
|
| 15 |
of related programs takes more work. Common.
|
| 16 |
|
| 17 |
+
**Change:** Type of *character-literal* is changed from `int` to `char`.
|
| 18 |
+
**Rationale:** This is needed for improved overloaded function argument
|
| 19 |
+
type matching. For example:
|
| 20 |
|
| 21 |
``` cpp
|
| 22 |
int function( int i );
|
| 23 |
int function( char c );
|
| 24 |
|
|
|
|
| 34 |
```
|
| 35 |
|
| 36 |
will not work the same as C++ programs. Simple. Programs which depend
|
| 37 |
upon `sizeof('x')` are probably rare.
|
| 38 |
|
| 39 |
+
**Change:** String literals made const.
|
| 40 |
+
The type of a *string-literal* is changed from “array of `char`” to
|
| 41 |
+
“array of `const char`”. The type of a UTF-8 string literal is changed
|
| 42 |
+
from “array of `char`” to “array of `const char8_t`”. The type of a
|
| 43 |
+
UTF-16 string literal is changed from “array of *some-integer-type*” to
|
| 44 |
+
“array of `const char16_t`”. The type of a UTF-32 string literal is
|
| 45 |
+
changed from “array of *some-integer-type*” to “array of
|
| 46 |
+
`const char32_t`”. The type of a wide string literal is changed from
|
| 47 |
+
“array of `wchar_t`” to “array of `const wchar_t`”. **Rationale:** This
|
| 48 |
+
avoids calling an inappropriate overloaded function, which might expect
|
| 49 |
+
to be able to modify its argument. **Effect on original feature:**
|
| 50 |
+
Change to semantics of well-defined feature. Syntactic transformation.
|
| 51 |
+
The fix is to add a cast:
|
| 52 |
|
| 53 |
``` cpp
|
| 54 |
+
char* p = "abc"; // valid in C, invalid in C++{}
|
| 55 |
void f(char*) {
|
| 56 |
char* p = (char*)"abc"; // OK: cast added
|
| 57 |
f(p);
|
| 58 |
f((char*)"def"); // OK: cast added
|
| 59 |
}
|
| 60 |
```
|
| 61 |
|
| 62 |
+
Programs that have a legitimate reason to treat string literal objects
|
| 63 |
+
as potentially modifiable memory are probably rare.
|
| 64 |
|
| 65 |
+
### [[basic]]: basics <a id="diff.basic">[[diff.basic]]</a>
|
| 66 |
|
| 67 |
+
**Change:** C++ does not have “tentative definitions” as in C.
|
|
|
|
| 68 |
E.g., at file scope,
|
| 69 |
|
| 70 |
``` cpp
|
| 71 |
int i;
|
| 72 |
int i;
|
| 73 |
```
|
| 74 |
|
| 75 |
is valid in C, invalid in C++. This makes it impossible to define
|
| 76 |
+
mutually referential file-local objects with static storage duration, if
|
| 77 |
+
initializers are restricted to the syntactic forms of C. For example,
|
| 78 |
|
| 79 |
``` cpp
|
| 80 |
struct X { int i; struct X* next; };
|
| 81 |
|
| 82 |
static struct X a;
|
|
|
|
| 86 |
|
| 87 |
**Rationale:** This avoids having different initialization rules for
|
| 88 |
fundamental types and user-defined types. **Effect on original
|
| 89 |
feature:** Deletion of semantically well-defined feature. Semantic
|
| 90 |
transformation. In C++, the initializer for one of a set of
|
| 91 |
+
mutually-referential file-local objects with static storage duration
|
| 92 |
+
must invoke a function call to achieve the initialization. Seldom.
|
| 93 |
|
| 94 |
+
**Change:** A `struct` is a scope in C++, not in C. **Rationale:** Class
|
| 95 |
+
scope is crucial to C++, and a struct is a class. **Effect on original
|
| 96 |
+
feature:** Change to semantics of well-defined feature. Semantic
|
| 97 |
+
transformation. C programs use `struct` extremely frequently, but the
|
| 98 |
+
change is only noticeable when `struct`, enumeration, or enumerator
|
| 99 |
+
names are referred to outside the `struct`. The latter is probably rare.
|
|
|
|
| 100 |
|
| 101 |
+
\[also [[dcl.type]]\] **Change:** A name of file scope that is
|
| 102 |
+
explicitly declared `const`, and not explicitly declared `extern`, has
|
| 103 |
+
internal linkage, while in C it would have external linkage.
|
| 104 |
+
**Rationale:** Because const objects may be used as values during
|
| 105 |
+
translation in C++, this feature urges programmers to provide an
|
| 106 |
+
explicit initializer for each const object. This feature allows the user
|
| 107 |
+
to put const objects in source files that are included in more than one
|
| 108 |
+
translation unit. **Effect on original feature:** Change to semantics of
|
| 109 |
+
well-defined feature. Semantic transformation. Seldom.
|
| 110 |
|
| 111 |
+
**Change:** The `main` function cannot be called recursively and cannot
|
| 112 |
+
have its address taken. **Rationale:** The `main` function may require
|
| 113 |
+
special actions. **Effect on original feature:** Deletion of
|
| 114 |
+
semantically well-defined feature. Trivial: create an intermediary
|
| 115 |
+
function such as `mymain(argc, argv)`. Seldom.
|
| 116 |
|
| 117 |
+
**Change:** C allows “compatible types” in several places, C++ does
|
| 118 |
+
not.
|
| 119 |
For example, otherwise-identical `struct` types with different tag names
|
| 120 |
are “compatible” in C but are distinctly different types in C++.
|
| 121 |
**Rationale:** Stricter type checking is essential for C++. **Effect on
|
| 122 |
original feature:** Deletion of semantically well-defined feature.
|
| 123 |
Semantic transformation. The “typesafe linkage” mechanism will find
|
| 124 |
many, but not all, of such problems. Those problems not found by
|
| 125 |
typesafe linkage will continue to function properly, according to the
|
| 126 |
+
“layout compatibility rules” of this document. Common.
|
| 127 |
|
| 128 |
+
### [[expr]]: expressions <a id="diff.expr">[[diff.expr]]</a>
|
| 129 |
|
| 130 |
+
**Change:** Converting `void*` to a pointer-to-object type requires
|
| 131 |
+
casting.
|
| 132 |
|
| 133 |
``` cpp
|
| 134 |
char a[10];
|
| 135 |
void* b=a;
|
| 136 |
void foo() {
|
| 137 |
char* c=b;
|
| 138 |
}
|
| 139 |
```
|
| 140 |
|
| 141 |
ISO C will accept this usage of pointer to void being assigned to a
|
| 142 |
+
pointer to object type. C++ will not. **Rationale:** C++ tries harder
|
| 143 |
+
than C to enforce compile-time type safety. **Effect on original
|
| 144 |
+
feature:** Deletion of semantically well-defined feature. Could be
|
| 145 |
+
automated. Violations will be diagnosed by the C++ translator. The fix
|
| 146 |
+
is to add a cast. For example:
|
| 147 |
|
| 148 |
``` cpp
|
| 149 |
char* c = (char*) b;
|
| 150 |
```
|
| 151 |
|
| 152 |
This is fairly widely used but it is good programming practice to add
|
| 153 |
the cast when assigning pointer-to-void to pointer-to-object. Some ISO C
|
| 154 |
translators will give a warning if the cast is not used.
|
| 155 |
|
| 156 |
+
**Change:** Implicit declaration of functions is not allowed.
|
| 157 |
+
**Rationale:** The type-safe nature of C++. **Effect on original
|
| 158 |
+
feature:** Deletion of semantically well-defined feature. Note: the
|
| 159 |
+
original feature was labeled as “obsolescent” in ISO C. Syntactic
|
|
|
|
|
|
|
| 160 |
transformation. Facilities for producing explicit function declarations
|
| 161 |
are fairly widespread commercially. Common.
|
| 162 |
|
| 163 |
+
**Change:** Decrement operator is not allowed with `bool` operand.
|
| 164 |
+
**Rationale:** Feature with surprising semantics. **Effect on original
|
| 165 |
+
feature:** A valid ISO C expression utilizing the decrement operator on
|
| 166 |
+
a `bool` lvalue (for instance, via the C typedef in ) is ill-formed in
|
| 167 |
+
this International Standard.
|
|
|
|
| 168 |
|
| 169 |
+
**Change:** Types must be defined in declarations, not in expressions.
|
|
|
|
| 170 |
In C, a sizeof expression or cast expression may define a new type. For
|
| 171 |
example,
|
| 172 |
|
| 173 |
``` cpp
|
| 174 |
p = (void*)(struct x {int i;} *)0;
|
|
|
|
| 177 |
defines a new type, struct `x`. **Rationale:** This prohibition helps to
|
| 178 |
clarify the location of definitions in the source code. **Effect on
|
| 179 |
original feature:** Deletion of semantically well-defined feature.
|
| 180 |
Syntactic transformation. Seldom.
|
| 181 |
|
| 182 |
+
**Change:** The result of a conditional expression, an assignment
|
| 183 |
+
expression, or a comma expression may be an lvalue. **Rationale:** C++
|
| 184 |
+
is an object-oriented language, placing relatively more emphasis on
|
| 185 |
+
lvalues. For example, function calls may yield lvalues. **Effect on
|
| 186 |
+
original feature:** Change to semantics of well-defined feature. Some C
|
| 187 |
+
expressions that implicitly rely on lvalue-to-rvalue conversions will
|
| 188 |
+
yield different results. For example,
|
| 189 |
|
| 190 |
``` cpp
|
| 191 |
char arr[100];
|
| 192 |
sizeof(0, arr)
|
| 193 |
```
|
| 194 |
|
| 195 |
yields `100` in C++ and `sizeof(char*)` in C. Programs must add explicit
|
| 196 |
casts to the appropriate rvalue. Rare.
|
| 197 |
|
| 198 |
+
### [[stmt.stmt]]: statements <a id="diff.stat">[[diff.stat]]</a>
|
| 199 |
|
| 200 |
+
**Change:** It is now invalid to jump past a declaration with explicit
|
| 201 |
+
or implicit initializer (except across entire block not entered).
|
| 202 |
+
**Rationale:** Constructors used in initializers may allocate resources
|
| 203 |
+
which need to be de-allocated upon leaving the block. Allowing jump past
|
| 204 |
+
initializers would require complicated runtime determination of
|
| 205 |
+
allocation. Furthermore, any use of the uninitialized object could be a
|
| 206 |
+
disaster. With this simple compile-time rule, C++ assures that if an
|
| 207 |
+
initialized variable is in scope, then it has assuredly been
|
| 208 |
+
initialized. **Effect on original feature:** Deletion of semantically
|
| 209 |
+
well-defined feature. Semantic transformation. Seldom.
|
|
|
|
| 210 |
|
| 211 |
+
**Change:** It is now invalid to return (explicitly or implicitly) from
|
| 212 |
+
a function which is declared to return a value without actually
|
| 213 |
+
returning a value. **Rationale:** The caller and callee may assume
|
| 214 |
+
fairly elaborate return-value mechanisms for the return of class
|
| 215 |
objects. If some flow paths execute a return without specifying any
|
| 216 |
value, the implementation must embody many more complications. Besides,
|
| 217 |
promising to return a value of a given type, and then not returning such
|
| 218 |
a value, has always been recognized to be a questionable practice,
|
| 219 |
tolerated only because very-old C had no distinction between void
|
|
|
|
| 221 |
semantically well-defined feature. Semantic transformation. Add an
|
| 222 |
appropriate return value to the source code, such as zero. Seldom. For
|
| 223 |
several years, many existing C implementations have produced warnings in
|
| 224 |
this case.
|
| 225 |
|
| 226 |
+
### [[dcl.dcl]]: declarations <a id="diff.dcl">[[diff.dcl]]</a>
|
| 227 |
|
| 228 |
+
**Change:** In C++, the `static` or `extern` specifiers can only be
|
| 229 |
+
applied to names of objects or functions.
|
| 230 |
Using these specifiers with type declarations is illegal in C++. In C,
|
| 231 |
these specifiers are ignored when used on type declarations.
|
| 232 |
|
| 233 |
Example:
|
| 234 |
|
| 235 |
``` cpp
|
| 236 |
+
static struct S { // valid C, invalid in C++{}
|
| 237 |
int i;
|
| 238 |
};
|
| 239 |
```
|
| 240 |
|
| 241 |
**Rationale:** Storage class specifiers don’t have any meaning when
|
|
|
|
| 243 |
`static` storage class specifier. Allowing storage class specifiers on
|
| 244 |
type declarations could render the code confusing for users. **Effect on
|
| 245 |
original feature:** Deletion of semantically well-defined feature.
|
| 246 |
Syntactic transformation. Seldom.
|
| 247 |
|
| 248 |
+
**Change:** In C++, `register` is not a storage class specifier.
|
| 249 |
+
**Rationale:** The storage class specifier had no effect in C++.
|
| 250 |
+
**Effect on original feature:** Deletion of semantically well-defined
|
| 251 |
+
feature. Syntactic transformation. Common.
|
| 252 |
|
| 253 |
+
**Change:** A C++ typedef name must be different from any class type
|
| 254 |
+
name declared in the same scope (except if the typedef is a synonym of
|
| 255 |
+
the class name with the same name). In C, a typedef name and a struct
|
| 256 |
+
tag name declared in the same scope can have the same name (because they
|
| 257 |
+
have different name spaces).
|
| 258 |
|
| 259 |
Example:
|
| 260 |
|
| 261 |
``` cpp
|
| 262 |
+
typedef struct name1 { ... } name1; // valid C and C++{}
|
| 263 |
struct name { ... };
|
| 264 |
+
typedef int name; // valid C, invalid C++{}
|
| 265 |
```
|
| 266 |
|
| 267 |
**Rationale:** For ease of use, C++ doesn’t require that a type name be
|
| 268 |
prefixed with the keywords `class`, `struct` or `union` when used in
|
| 269 |
object declarations or type casts.
|
|
|
|
| 277 |
|
| 278 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 279 |
feature. Semantic transformation. One of the 2 types has to be renamed.
|
| 280 |
Seldom.
|
| 281 |
|
| 282 |
+
\[see also [[basic.link]]\] **Change:** Const objects must be
|
| 283 |
+
initialized in C++ but can be left uninitialized in C. **Rationale:** A
|
| 284 |
+
const object cannot be assigned to so it must be initialized to hold a
|
| 285 |
+
useful value. **Effect on original feature:** Deletion of semantically
|
| 286 |
+
well-defined feature. Semantic transformation. Seldom.
|
|
|
|
| 287 |
|
| 288 |
+
**Change:** Banning implicit `int`.
|
| 289 |
|
| 290 |
In C++ a *decl-specifier-seq* must contain a *type-specifier*, unless it
|
| 291 |
is followed by a declarator for a constructor, a destructor, or a
|
| 292 |
conversion function. In the following example, the left-hand column
|
| 293 |
presents valid C; the right-hand column presents equivalent C++:
|
|
|
|
| 305 |
proper style. Liaison with WG14 (C) indicated support for (at least)
|
| 306 |
deprecating implicit int in the next revision of C. **Effect on original
|
| 307 |
feature:** Deletion of semantically well-defined feature. Syntactic
|
| 308 |
transformation. Could be automated. Common.
|
| 309 |
|
| 310 |
+
**Change:** The keyword `auto` cannot be used as a storage class
|
| 311 |
+
specifier.
|
| 312 |
|
| 313 |
``` cpp
|
| 314 |
void f() {
|
| 315 |
+
auto int x; // valid C, invalid C++{}
|
| 316 |
}
|
| 317 |
```
|
| 318 |
|
| 319 |
**Rationale:** Allowing the use of `auto` to deduce the type of a
|
| 320 |
variable from its initializer results in undesired interpretations of
|
| 321 |
`auto` as a storage class specifier in certain contexts. **Effect on
|
| 322 |
original feature:** Deletion of semantically well-defined feature.
|
| 323 |
Syntactic transformation. Rare.
|
| 324 |
|
| 325 |
+
**Change:** In C++, a function declared with an empty parameter list
|
| 326 |
+
takes no arguments. In C, an empty parameter list means that the number
|
| 327 |
+
and type of the function arguments are unknown.
|
| 328 |
|
| 329 |
Example:
|
| 330 |
|
| 331 |
``` cpp
|
| 332 |
+
int f(); // means int f(void) in C++{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
// int f( unknown ) in C
|
| 334 |
```
|
| 335 |
|
| 336 |
**Rationale:** This is to avoid erroneous function calls (i.e., function
|
| 337 |
calls with the wrong number or type of arguments). **Effect on original
|
|
|
|
| 341 |
become full prototype declarations. A program may need to be updated
|
| 342 |
further if different calls to the same (non-prototype) function have
|
| 343 |
different numbers of arguments or if the type of corresponding arguments
|
| 344 |
differed. Common.
|
| 345 |
|
| 346 |
+
\[see [[expr.sizeof]]\] **Change:** In C++, types may not be defined in
|
| 347 |
+
return or parameter types. In C, these type definitions are allowed.
|
|
|
|
| 348 |
|
| 349 |
Example:
|
| 350 |
|
| 351 |
``` cpp
|
| 352 |
+
void f( struct S { int a; } arg ) {} // valid C, invalid C++{}
|
| 353 |
+
enum E { A, B, C } f() {} // valid C, invalid C++{}
|
| 354 |
```
|
| 355 |
|
| 356 |
+
**Rationale:** When comparing types in different translation units, C++
|
| 357 |
+
relies on name equivalence when C relies on structural equivalence.
|
| 358 |
Regarding parameter types: since the type defined in a parameter list
|
| 359 |
would be in the scope of the function, the only legal calls in C++ would
|
| 360 |
be from within the function itself. **Effect on original feature:**
|
| 361 |
Deletion of semantically well-defined feature. Semantic transformation.
|
| 362 |
The type definitions must be moved to file scope, or in header files.
|
| 363 |
Seldom. This style of type definition is seen as poor coding style.
|
| 364 |
|
| 365 |
+
**Change:** In C++, the syntax for function definition excludes the
|
| 366 |
+
“old-style” C function. In C, “old-style” syntax is allowed, but
|
| 367 |
+
deprecated as “obsolescent”. **Rationale:** Prototypes are essential to
|
| 368 |
+
type safety. **Effect on original feature:** Deletion of semantically
|
| 369 |
+
well-defined feature. Syntactic transformation. Common in old programs,
|
| 370 |
+
but already known to be obsolescent.
|
| 371 |
|
| 372 |
+
**Change:** In C++, designated initialization support is restricted
|
| 373 |
+
compared to the corresponding functionality in C. In C++, designators
|
| 374 |
+
for non-static data members must be specified in declaration order,
|
| 375 |
+
designators for array elements and nested designators are not supported,
|
| 376 |
+
and designated and non-designated initializers cannot be mixed in the
|
| 377 |
+
same initializer list.
|
| 378 |
|
| 379 |
Example:
|
| 380 |
|
| 381 |
``` cpp
|
| 382 |
+
struct A { int x, y; };
|
| 383 |
+
struct B { struct A a; };
|
| 384 |
+
struct A a = {.y = 1, .x = 2}; // valid C, invalid C++{}
|
| 385 |
+
int arr[3] = {[1] = 5}; // valid C, invalid C++{}
|
| 386 |
+
struct B b = {.a.x = 0}; // valid C, invalid C++{}
|
| 387 |
+
struct A c = {.x = 1, 2}; // valid C, invalid C++{}
|
| 388 |
+
```
|
| 389 |
+
|
| 390 |
+
**Rationale:** In C++, members are destroyed in reverse construction
|
| 391 |
+
order and the elements of an initializer list are evaluated in lexical
|
| 392 |
+
order, so field initializers must be specified in order. Array
|
| 393 |
+
designators conflict with *lambda-expression* syntax. Nested designators
|
| 394 |
+
are seldom used. **Effect on original feature:** Deletion of feature
|
| 395 |
+
that is incompatible with C++. Syntactic transformation. Out-of-order
|
| 396 |
+
initializers are common. The other features are seldom used.
|
| 397 |
+
|
| 398 |
+
**Change:** In C++, when initializing an array of character with a
|
| 399 |
+
string, the number of characters in the string (including the
|
| 400 |
+
terminating `'\0'`) must not exceed the number of elements in the array.
|
| 401 |
+
In C, an array can be initialized with a string even if the array is not
|
| 402 |
+
large enough to contain the string-terminating `'\0'`.
|
| 403 |
+
|
| 404 |
+
Example:
|
| 405 |
+
|
| 406 |
+
``` cpp
|
| 407 |
+
char array[4] = "abcd"; // valid C, invalid C++{}
|
| 408 |
```
|
| 409 |
|
| 410 |
**Rationale:** When these non-terminated arrays are manipulated by
|
| 411 |
standard string functions, there is potential for major catastrophe.
|
| 412 |
**Effect on original feature:** Deletion of semantically well-defined
|
| 413 |
feature. Semantic transformation. The arrays must be declared one
|
| 414 |
element bigger to contain the string terminating `'\0'`. Seldom. This
|
| 415 |
style of array initialization is seen as poor coding style.
|
| 416 |
|
| 417 |
+
**Change:** C++ objects of enumeration type can only be assigned values
|
| 418 |
+
of the same enumeration type. In C, objects of enumeration type can be
|
| 419 |
+
assigned values of any integral type.
|
| 420 |
+
|
| 421 |
+
Example:
|
| 422 |
+
|
| 423 |
+
``` cpp
|
| 424 |
+
enum color { red, blue, green };
|
| 425 |
+
enum color c = 1; // valid C, invalid C++{}
|
| 426 |
+
```
|
| 427 |
+
|
| 428 |
+
**Rationale:** The type-safe nature of C++. **Effect on original
|
| 429 |
+
feature:** Deletion of semantically well-defined feature. Syntactic
|
| 430 |
+
transformation. (The type error produced by the assignment can be
|
| 431 |
+
automatically corrected by applying an explicit cast.) Common.
|
| 432 |
+
|
| 433 |
+
**Change:** In C++, the type of an enumerator is its enumeration. In C,
|
| 434 |
+
the type of an enumerator is `int`.
|
| 435 |
+
|
| 436 |
+
Example:
|
| 437 |
+
|
| 438 |
+
``` cpp
|
| 439 |
+
enum e { A };
|
| 440 |
+
sizeof(A) == sizeof(int) // in C
|
| 441 |
+
sizeof(A) == sizeof(e) // in C++{}
|
| 442 |
+
/* and sizeof(int) is not necessarily equal to sizeof(e) */
|
| 443 |
+
```
|
| 444 |
+
|
| 445 |
+
**Rationale:** In C++, an enumeration is a distinct type. **Effect on
|
| 446 |
+
original feature:** Change to semantics of well-defined feature.
|
| 447 |
+
Semantic transformation. Seldom. The only time this affects existing C
|
| 448 |
+
code is when the size of an enumerator is taken. Taking the size of an
|
| 449 |
+
enumerator is not a common C coding practice.
|
| 450 |
+
|
| 451 |
+
### [[class]]: classes <a id="diff.class">[[diff.class]]</a>
|
| 452 |
+
|
| 453 |
+
\[see also [[dcl.typedef]]\] **Change:** In C++, a class declaration
|
| 454 |
+
introduces the class name into the scope where it is declared and hides
|
| 455 |
+
any object, function or other declaration of that name in an enclosing
|
| 456 |
+
scope. In C, an inner scope declaration of a struct tag name never hides
|
| 457 |
+
the name of an object or function in an outer scope.
|
| 458 |
|
| 459 |
Example:
|
| 460 |
|
| 461 |
``` cpp
|
| 462 |
int x[99];
|
| 463 |
void f() {
|
| 464 |
struct x { int a; };
|
| 465 |
sizeof(x); /* size of the array in C */
|
| 466 |
+
/* size of the struct in \textit{\textrm{C++{}}} */
|
| 467 |
}
|
| 468 |
```
|
| 469 |
|
| 470 |
**Rationale:** This is one of the few incompatibilities between C and
|
| 471 |
C++ that can be attributed to the new C++ name space definition where a
|
|
|
|
| 477 |
user-defined types as similar as possible to the use of fundamental
|
| 478 |
types. The advantages of the new name space definition were judged to
|
| 479 |
outweigh by far the incompatibility with C described above. **Effect on
|
| 480 |
original feature:** Change to semantics of well-defined feature.
|
| 481 |
Semantic transformation. If the hidden name that needs to be accessed is
|
| 482 |
+
at global scope, the `::` C++ operator can be used. If the hidden name
|
| 483 |
+
is at block scope, either the type or the struct tag has to be renamed.
|
| 484 |
Seldom.
|
| 485 |
|
| 486 |
+
**Change:** Copying volatile objects.
|
| 487 |
+
|
| 488 |
+
The implicitly-declared copy constructor and implicitly-declared copy
|
| 489 |
+
assignment operator cannot make a copy of a volatile lvalue. For
|
| 490 |
+
example, the following is valid in ISO C:
|
| 491 |
+
|
| 492 |
+
``` cpp
|
| 493 |
+
struct X { int i; };
|
| 494 |
+
volatile struct X x1 = {0};
|
| 495 |
+
struct X x2 = x1; // invalid C++{}
|
| 496 |
+
struct X x3;
|
| 497 |
+
x3 = x1; // also invalid C++{}
|
| 498 |
+
```
|
| 499 |
+
|
| 500 |
+
**Rationale:** Several alternatives were debated at length. Changing the
|
| 501 |
+
parameter to `volatile` `const` `X&` would greatly complicate the
|
| 502 |
+
generation of efficient code for class objects. Discussion of providing
|
| 503 |
+
two alternative signatures for these implicitly-defined operations
|
| 504 |
+
raised unanswered concerns about creating ambiguities and complicating
|
| 505 |
+
the rules that specify the formation of these operators according to the
|
| 506 |
+
bases and members. **Effect on original feature:** Deletion of
|
| 507 |
+
semantically well-defined feature. Semantic transformation. If volatile
|
| 508 |
+
semantics are required for the copy, a user-declared constructor or
|
| 509 |
+
assignment must be provided. If non-volatile semantics are required, an
|
| 510 |
+
explicit `const_cast` can be used. Seldom.
|
| 511 |
+
|
| 512 |
+
**Change:** Bit-fields of type plain `int` are signed. **Rationale:**
|
| 513 |
+
Leaving the choice of signedness to implementations could lead to
|
| 514 |
+
inconsistent definitions of template specializations. For consistency,
|
| 515 |
+
the implementation freedom was eliminated for non-dependent types, too.
|
| 516 |
+
**Effect on original feature:** The choice is implementation-defined in
|
| 517 |
+
C, but not so in C++. Syntactic transformation. Seldom.
|
| 518 |
+
|
| 519 |
+
**Change:** In C++, the name of a nested class is local to its enclosing
|
| 520 |
+
class. In C the name of the nested class belongs to the same scope as
|
| 521 |
+
the name of the outermost enclosing class.
|
| 522 |
|
| 523 |
Example:
|
| 524 |
|
| 525 |
``` cpp
|
| 526 |
struct X {
|
| 527 |
struct Y { ... } y;
|
| 528 |
};
|
| 529 |
+
struct Y yy; // valid C, invalid C++{}
|
| 530 |
```
|
| 531 |
|
| 532 |
**Rationale:** C++ classes have member functions which require that
|
| 533 |
classes establish scopes. The C rule would leave classes as an
|
| 534 |
incomplete scope mechanism which would prevent C++ programmers from
|
|
|
|
| 552 |
definitions and accessed outside the scope of the enclosing struct could
|
| 553 |
be exported to the scope of the enclosing struct. Note: this is a
|
| 554 |
consequence of the difference in scope rules, which is documented in
|
| 555 |
[[basic.scope]]. Seldom.
|
| 556 |
|
| 557 |
+
**Change:** In C++, a typedef name may not be redeclared in a class
|
| 558 |
+
definition after being used in that definition.
|
| 559 |
|
| 560 |
Example:
|
| 561 |
|
| 562 |
``` cpp
|
| 563 |
typedef int I;
|
| 564 |
struct S {
|
| 565 |
I i;
|
| 566 |
+
int I; // valid C, invalid C++{}
|
| 567 |
};
|
| 568 |
```
|
| 569 |
|
| 570 |
**Rationale:** When classes become complicated, allowing such a
|
| 571 |
redefinition after the type has been used can create confusion for C++
|
| 572 |
programmers as to what the meaning of `I` really is. **Effect on
|
| 573 |
original feature:** Deletion of semantically well-defined feature.
|
| 574 |
Semantic transformation. Either the type or the struct member has to be
|
| 575 |
renamed. Seldom.
|
| 576 |
|
| 577 |
+
### [[cpp]]: preprocessing directives <a id="diff.cpp">[[diff.cpp]]</a>
|
| 578 |
+
|
| 579 |
+
**Change:** Whether `__STDC__` is defined and if so, what its value is,
|
| 580 |
+
are *implementation-defined*. **Rationale:** C++ is not identical to ISO
|
| 581 |
+
C. Mandating that `__STDC__` be defined would require that translators
|
| 582 |
+
make an incorrect claim. Each implementation must choose the behavior
|
| 583 |
+
that will be most useful to its marketplace. **Effect on original
|
| 584 |
+
feature:** Change to semantics of well-defined feature. Semantic
|
| 585 |
+
transformation. Programs and headers that reference `__STDC__` are quite
|
| 586 |
+
common.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
|