Comments (3)
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 am not sure about that.
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 elementv[0]
having been initialized fromv
. I think this can be surprising to most programmers.
I think w[0]
was meant in place of v[0]
.
What do you think wouldn't surprise most programmers in this case?
In my opinion based on looking at it now, it looks like an artificially convoluted piece of code (in order to define a - single-element - vector, of vector(s), of anys), but I think it looks like the expected behavior.
from cppcoreguidelines.
I think w[0] was meant in place of v[0].
Yes, sorry.
What do you think wouldn't surprise most programmers in this case?
On the contrary, I think that what happens with std::vector<std::any> w{v};
would surprise most programmers at first.
What I'm saying is that it would beneficial if ES.23 mentioned that there are cases where preferring {}
-initializer syntax results in not very easy to debug bugs.
it looks like an artificially convoluted piece of code
Ok, to be more concrete, I had to update a class that looked like this,
struct Foo {
Foo(v, /* other args */)
: v{}
, /* other init.ons */
std::vector<std::any> v;
// other member vars + API
};
where v
would then be altered via some API. This all worked fine. the {}
instead of ()
in v{}
was out of the habit of abiding by ES.23.
Later, the need arised for v
to be initialized from some argument:
struct Foo {
Foo(v, /* other args */)
: v{std::move(v)}
, /* other init.ons */
std::vector<std::any> v;
// other member vars and member funcs
};
But this code is broken for the reason I explained, and it's not easy to debug because you just start seeing std::any_cast
fail and you don't immediately understand (I'm sure I'm passing a std::vector<Bar>
to Foo
's ctor, so why can't I cast an element of v
to Bar
?).
Another real-life case. Take this talk from Sean Parent about Runtime Polymorphism, where he always uses ()
-initialization instead of {}
-initialization. When I tried to rewrite that code myself, I came up with this, which is fundamentally his code, but with {}
instead of ()
for initalizing things, becuase that's my habit, i.e. follow ES.23.
The result is that the code is bugged on lines 11-12, and the behavior is a neverending recursive call that stackoverflows.
Making the ctor explicit
would have prevented the problem in the first place. But without that, it's clear that the {}
is not really resulting in what one generally expects if they write self{std::make_unique<model<T>>(std::move(t))}
.
from cppcoreguidelines.
The same point is made in https://abseil.io/tips/88
from cppcoreguidelines.
Related Issues (20)
- Sss
- Practice HOT 1
- #include HOT 1
- How to refer to the guidelines, bibtex preferred? HOT 3
- I.2: Avoid non-const global variables, but exempt "file scope" from this rule HOT 2
- C++ Core Guidelines Markdown to HTML HOT 1
- Why isn't the exception safety mentioned in the Reason of R.22? HOT 4
- C++ Core Guidelines Markdown explicit anchor links not working (updated the 20/05/2024)
- Doesn't the title of ES.3 break that very guideline itself? HOT 2
- Section E.27 HOT 1
- Ceil Function in Cpp HOT 3
- Namespace guidelines HOT 2
- Recommend against using RAII only for release functions that can fail, including all IO HOT 1
- Clarify ES.23: Prefer the {}-initializer syntax HOT 1
- Exception to ES.63 for base type without any data member HOT 1
- Discourage implicit casting from/to void pointer HOT 1
- Should SF.7 avoid to limit its applicability to when the directive is at global scope?
- the links in I: Interfaces are all broken HOT 1
- gsl::span UB if used over containers of non-implicit lifetime types? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppcoreguidelines.