isocpp / isocpp/CppCoreGuidelines

Should ES.23 mention that ()-initialization is likely to be preferrable when dealing with containers containing types initializable from the container itself

Open Beginner friendly
#2,205 8 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

Here's ES.23.

Should that mention that one might want to prefer ()-initialization to {}-initialization when initializing a container of elements of a given type from a container of the same type, when the inner type can be constructed from the vector itself?

I'm referring to the fact that

std::vector<std::any> v{1,2,3};
std::vector<std::any> w{v};

will result in w having length 1, with its only element v[0] having been initialized from v. I think this can be surprising to most programmers.

A similar scenario happens when, instead of std::any, the type inside the container has a templated constructor, like in this case.

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 with ES.23 in the linked C++ Core Guidelines and review the examples involving brace initialization of containers. Clarify whether the guidance should mention preferring ()-initialization when an element type can be constructed from the container itself, using the std::vectorstd::any example to verify the wording and outcome.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.