Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[basic.start.dynamic] p5 Bad example for deferred initialization #4356

Open
xmh0511 opened this issue Nov 12, 2020 · 1 comment
Open

[basic.start.dynamic] p5 Bad example for deferred initialization #4356

xmh0511 opened this issue Nov 12, 2020 · 1 comment
Labels
cwg Issue must be reviewed by CWG.

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Nov 12, 2020

// - File 1 -
#include "a.h"
#include "b.h"
B b;
A::A(){
  b.Use();
}

// - File 2 -
#include "a.h"
A a;

// - File 3 -
#include "a.h"
#include "b.h"
extern A a;
extern B b;

int main() {
  a.Use();
  b.Use();
}

For this example, The interpretation for the deferred dynamic initialization may exist something wrong. The comment is:

If, however, a is initialized at some point after the first statement of main, b will be initialized prior to its use in A​::​A.

In other words, the sentence interprets that when the dynamic initialization is deferred, how to initialize these deferred variable. Although, the non-inline function A::A() is defined at the same TU as the variable b, however the invocation of A::A() is caused by the initialization of a, in other words, the use of A::A() shouldn't be considered as non-initialization odr-use, whose term definition is:

A non-initialization odr-use is an odr-use ([basic.def.odr]) not caused directly or indirectly by the initialization of a non-local static or thread storage duration variable.

Hence, in this situation, the use of A::A() shouldn't cause the initialization of the variable b. The use of b in expression b.Use() is caused indirectly by the initialization of the variable a(as aforementioned), which shouldn't trigger the initialization of the variable b. So, why the comment says that b will be initialized prior to its use in A​::​A?

Such an issues has been discussed at here.

@jensmaurer jensmaurer changed the title Might be a wrong of the interpretation of the example in [basic.start.dynamic#5] [basic.start.dynamic] p5 Bad example for deferred initialization Dec 29, 2020
@jensmaurer
Copy link
Member

@jensmaurer jensmaurer added the cwg Issue must be reviewed by CWG. label Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG.
Projects
None yet
Development

No branches or pull requests

2 participants