From 7820e74b0d4ac410698331f8977fb2b8e0df4416 Mon Sep 17 00:00:00 2001 From: Enrico Maria De Angelis Date: Tue, 10 Sep 2024 18:14:59 +0100 Subject: [PATCH] Update CppCoreGuidelines.md Addressing the issue #2205 I opened myself. I can't see why not contributing. --- CppCoreGuidelines.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 8589d784b..a179b00ff 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -11165,6 +11165,16 @@ Use plain `{}`-initialization unless you specifically want to disable explicit c // ... } +##### Exception + +For container-like types whose element type can be initialized from an instance of that very container +type iteself, e.g. `std::any` or a template parameter `T` of a template type having a constructor accepting `T`, +use `()` to avoid erroneously constructing a nested container: + + vector v{1,2,3}; + vector u{v}; // most likely unintended: u has 1 element of type any, which contains v + vector w(v); // w is a copy of v + **See also**: [Discussion](#???) ##### Enforcement