cplusplus / cplusplus/draft

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

Open
#4,356 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cwg
Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

// - 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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the [basic.start.dynamic] wording and the three-file example in this issue, then compare the interpretation with the linked Stack Overflow discussion. Resolve whether the comment correctly describes deferred initialization and, if not, identify the wording or example that needs correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.