Clarification of rationale for not having tentative definitions

Viewed 58

C++17 (N4713), C.1.2 Clause 6: basic concepts, 1:

Change: C++ does not have “tentative definitions” as in C.

Rationale: This avoids having different initialization rules for fundamental types and user-defined types.

Question: what are the different initialization rules for fundamental types and user-defined types? Any examples?


Extra: here are the mutually referential file-local static objects in C:

struct X { int i; struct X* next; };
static struct X a;
static struct X b = { 0, &a };
static struct X a = { 1, &b };

In C++ this code is invalid. How to achieve the same in C++?

1 Answers

The preferred way to declare things with file scope is an anonymous namespace. Everything inside namespace {} has file scope. You can declare functions, classes, variables, etc. extern works as usual to declare a.

Note that in C++ it's not necessary to write struct X

struct X { int i; X* next; };
namespace {
    extern X a;
    X b = { 0, &a };
    X a = { 1, &b };
}
Related