Skip to content

Prefer default member initializers also for non-constant initializers - #2333

Closed
N-Dekker wants to merge 1 commit into
isocpp:masterfrom
N-Dekker:C-48-Prefer-default-member-initializers-also-for-non-constant
Closed

N-Dekker wants to merge 1 commit into
isocpp:masterfrom
N-Dekker:C-48-Prefer-default-member-initializers-also-for-non-constant

Conversation

@N-Dekker

@N-Dekker N-Dekker commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Removed the restriction "for constant initializers" from guideline C.48, Prefer default member initializers to member initializers in constructors for constant initializers

    class MyContainer {
        time_point<system_clock> creationTime{ system_clock::now() };  // Preferable, right?
        vector<int> data{};
    public:
        MyContainer() = default;
        explicit MyContainer(size_t n) : data(n) {}
        MyContainer(initializer_list<int> values) : data{ values } {}
    };

Removed the restriction "for constant initializers" from guideline C.48, "Prefer default member initializers to member initializers in constructors..."

The restriction appears unnecessary, as discussed by issue isocpp#2332
@hsutter

hsutter commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Editors call: Thanks!

Regarding numeric_limits<T>::min, that is constexpr so it's not equivalent to now().

We do want to avoid dynamic initialization in general wherever possible.

We think the existing rule is right for the majority of code, and if you have a reason to initialize using something like system_clock::now() or thread::get_id() then we recommend putting it in the constructor which makes it more obvious that something dynamic is happening.

@hsutter hsutter closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants