isocpp / isocpp/CppCoreGuidelines

I.22 Various issues

Open
#2,121 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
45.3k
Forks
5.6k
PR merge metrics
No merged PRs in 30d

Description

Firstly, the explanation in I.22 says that one of the initializations access an uninitialized object. I believe that this is not correct, and instead, a zero-initialized object is accessed.

It's somewhat unclear whether "complex initialization" is supposed to mean dynamic initialization, though I would assume that it is intended to.

Dynamic initialization is not a problem in principle, and it can be quite beneficial. For example:

inline const std::array<double, 128> sin_table = [] {
    decltype(sin_table) result;
    for (std::size_t = 0; i < sin_table.size(); ++i) {
        result[i] = std::sin(std::numbers::pi * 2 * i / sin_table.size());
    }
    return sin_table;
}();

Until C++26, sin_table could not be constexpr, yet it's entirely reasonable and even best practice to use dynamic initialization, since the only alternative is hand-writing the initializer-list, or writing your own constexpr sin function1). Neither of these are very practical.

1)This would be a solution because only non-constexpr functions are meant to be flagged by the current Enforcement standards

Suggested Solution

(Easy) use zero-initialization and dynamic initialization correctly in the current wording, and add an exception for dynamic initialization that only accesses objects within the current TU, or functions which act as a "pseudo-constructor", such as the lambda above.

(Hard) additionally reword the rule in terms of Avoid dynamic initialization which relies on objects in other translation units

Contributor guide

Open the contributing guide

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 by reading the I.22 rule linked in the issue and reviewing the distinction between zero-initialization and dynamic initialization. Compare the current wording with the suggested exception and broader rewording; done means the guideline accurately describes the initialization hazard and clearly defines any permitted cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.