TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

I have no constructor, and I must initialize

325 点作者 cyber111 个月前

24 条评论

gpderetta11 个月前
&gt; [...] The printed result would be 0. This is because we value-initialize t and, since T has a non-user-provided default constructor, the object is zero-initialized (hence t.x is zero-initialized) then default-initialized (calling the implicitly-defined default constructor, which does nothing).<p>T̶h̶a̶t̶ d̶o̶e̶s̶n̶&#x27;t̶ s̶e̶e̶m̶ c̶o̶r̶r̶e̶c̶t̶:̶ a̶ d̶e̶f̶a̶u̶l̶t̶e̶d̶ c̶o̶n̶s̶t̶r̶u̶c̶t̶o̶r̶ s̶t̶i̶l̶l̶ d̶e̶f̶a̶u̶l̶t̶-̶i̶n̶i̶t̶i̶a̶l̶i̶z̶e̶s̶ t̶h̶e̶ m̶e̶m̶b̶e̶r̶s̶, n̶o̶t̶ v̶a̶l̶u̶e̶ i̶n̶i̶t̶i̶a̶l̶i̶z̶e̶. I̶ d̶o̶n̶&#x27;t̶ t̶h̶i̶n̶k̶ t̶h̶e̶r̶e̶ i̶s̶ a̶n̶y̶ d̶i̶f̶f̶e̶r̶e̶n̶c̶e̶ b̶e̶t̶w̶e̶e̶n̶ d̶e̶f̶a̶u̶l̶t̶i̶n̶g̶ i̶n̶l̶i̶n̶e̶ a̶n̶d̶ o̶u̶t̶ o̶f̶ l̶i̶n̶e̶. G̶C̶C̶ s̶e̶e̶m̶s̶ t̶o̶ a̶g̶r̶e̶e̶:̶ h̶t̶t̶p̶s̶:̶&#x2F;&#x2F;g̶c̶c̶.g̶o̶d̶b̶o̶l̶t̶.o̶r̶g̶&#x2F;z̶&#x2F;r̶4̶r̶e̶5̶T̶E̶5̶a̶<p>edit: I missed that the author is actually value-initializing x!!! The result definitely violates expectations!<p>Generally, the details of the rules are arcane and sometimes have non-sensical dark corners having been extended and patched up for the last 40 years. But 99.9%[1] of the time you get what you expect.<p>I big improvement would be making default initialization explicit, and otherwise always value initialize. Explicit value initialization is so common that the very rare times I want default initialization (to avoid expensively zeroing large arrays) I need to write a fat comment. Writing &quot;std::array&lt;int, 100&gt; = void;&quot; (or whatever the syntax would be) would be much better.<p>[1] I had an extra 9 here... I hedged.
评论 #40882858 未加载
评论 #40888308 未加载
nickysielicki11 个月前
The whole thing is wrong. Don’t put const references in your structs. Use std::reference_wrapper if you must.<p>Edit: this response is a bit dismissive but honestly my main beef with this article is that its conclusion is just straight up wrong. Do not write your own constructors, do follow the rule of 5&#x2F;3&#x2F;0, and if you find yourself needing to hold a const reference, you should look out for whether you’re passing in an rval temporary… none of this is really scary.
评论 #40888366 未加载
评论 #40888299 未加载
评论 #40887903 未加载
bookofjoe11 个月前
&gt;I Have No Mouth, and I Must Scream (1967)<p><a href="https:&#x2F;&#x2F;talesofmytery.blogspot.com&#x2F;2018&#x2F;10&#x2F;harlan-ellison-i-have-no-mouth-and-i.html" rel="nofollow">https:&#x2F;&#x2F;talesofmytery.blogspot.com&#x2F;2018&#x2F;10&#x2F;harlan-ellison-i-...</a>
评论 #40892006 未加载
amluto11 个月前
I’m surprised there were no snarky comments about:<p>&gt; So, here’s the glue between list-initialization and aggregate initialization: if list-initialization is performed on an aggregate, aggregate initialization is performed <i>unless</i> the list has only one argument, of type T or of type derived from T, in which case it performs direct-initialization (or copy-initialization).<p>The word “unless” is even bold.<p>We have fancy syntax:<p><pre><code> T t{v0}; </code></pre> And we also have:<p><pre><code> T t{v0, v1}; </code></pre> And so on. But the one-element case does not reliably work like the 2+-element case. And this is in a language that increasingly works toward making it straightforward to create a struct from a parameter pack and has support for variable length array-ish things that one can initialize like this. And the types can, of course, be templated.<p>So you can write your own constructors, and you can initialize a tuple or array with only one element supplied, and you might trip over the <i>wrong</i> constructor being invoked in special cases.<p>I remember discovering this when C++11 initializer lists were brand new and thinking it was nuts.
评论 #40884419 未加载
AlexandrB11 个月前
For more C++ wackiness, I recommend the C++ FQA: <a href="https:&#x2F;&#x2F;yosefk.com&#x2F;c++fqa&#x2F;" rel="nofollow">https:&#x2F;&#x2F;yosefk.com&#x2F;c++fqa&#x2F;</a><p>It&#x27;s 15 years out of date now, but also timeless since C++ rarely&#x2F;never removes old features or behaviours.
评论 #40884792 未加载
gattilorenz11 个月前
What an beautiful blog theme, obviously inspired by the DEC-era computers but also clean and minimal. Refreshing!
评论 #40884941 未加载
评论 #40884508 未加载
IAmLiterallyAB10 个月前
Horrific. One of the things that scares me about C++. A real shame because it has some cool features and some brilliant minds working on it.<p>I&#x27;m hoping something like Herb&#x27;s C++ syntax 2 will make the language useable for mortals like me.
评论 #40888284 未加载
jakewins11 个月前
Man I get vertigo reading this. Reminds me of trying to understand Java constructors and object initialisation.<p>It’s been a while now, and at least in my experience so far Go and Rusts choice of not having special constructors really simplifies a lot.<p>Is there anyone that’s had the experience of missing constructors once you swapped away from them?
评论 #40882037 未加载
评论 #40888799 未加载
评论 #40882036 未加载
评论 #40887794 未加载
jolj11 个月前
Is there a C++ tool that adds&#x2F;shows all the implicit stuff that happens behind the scenes?<p>Such as all the constructors that are being added, implicit copy constructor and all the other surprises?
评论 #40883547 未加载
OptionOfT11 个月前
<p><pre><code> T::T() = default; </code></pre> &gt; You’d expect the printed result to be 0, right? You poor thing. Alas—it will be garbage. Some things can never be perfect, it seems. Here’s a relevant excerpt from our description of value-initialization:<p>Link: <a href="https:&#x2F;&#x2F;consteval.ca&#x2F;2024&#x2F;07&#x2F;03&#x2F;initialization&#x2F;#:~:text=You%E2%80%99d%20expect%20the%20printed%20result%20to%20be%200%2C%20right%3F%20You%20poor%20thing.%20Alas%E2%80%94it%20will%20be%20garbage.%20Some%20things%20can%20never%20be%20perfect%2C%20it%20seems.%20Here%E2%80%99s%20a%20relevant%20excerpt%20from%20our%20description%20of%20value%2Dinitialization%3A" rel="nofollow">https:&#x2F;&#x2F;consteval.ca&#x2F;2024&#x2F;07&#x2F;03&#x2F;initialization&#x2F;#:~:text=You%...</a><p>That actually isn&#x27;t that weird, because it would allow any consumer of your library to change how your library behaves.
评论 #40886969 未加载
marton7811 个月前
After almost 20 years of experience with C++, there are still some gnarly details I wouldn&#x27;t have imagined. What a God awful language!<p>Kudos to that author for the great, eye catching title and the in depth detail!
评论 #40882247 未加载
评论 #40881992 未加载
评论 #40882918 未加载
评论 #40882919 未加载
评论 #40883172 未加载
评论 #40885544 未加载
评论 #40882476 未加载
echelon11 个月前
A language shouldn&#x27;t be this complicated. This is dangerous and impossible for teams full of juniors and busy people with deadlines. We&#x27;re only human.
评论 #40882082 未加载
评论 #40882237 未加载
评论 #40881968 未加载
评论 #40886760 未加载
评论 #40882290 未加载
评论 #40887951 未加载
评论 #40882353 未加载
kazinator11 个月前
&gt; <i>Otherwise, zero-initialize and then default-initialize.</i><p>That can&#x27;t be right ... is it? Things cannot be initialized twice. Isn&#x27;t it more like &quot;Otherwise, recurse the value-initialization over the bases and members&quot;. Then, those that are not classes or arrays get zero-initialized.
评论 #40882948 未加载
评论 #40882505 未加载
shadowgovt11 个月前
Realizing the spec for C++23 is about two-and-some-change-times the length of the King James Bible has really reframed my thinking on this language.<p>For good or for ill, I don&#x27;t really trust <i>anything</i> that long to be something the average human can wrestle down. And when you mix in undefined behavior and the fact it&#x27;s still used on safety-critical systems... It&#x27;s a bit horrifying really.
评论 #40915692 未加载
wateralien11 个月前
Upvote for the title.
pierrebai11 个月前
The article is just plain wrong about classes: if you have declared any constructor, then the language will <i>not</i> provide a default constructor and default-initialization will fail with a compiler diagnostic.<p>So their claim that &quot;T t;&quot; will &quot;do nothing&quot; is incorrect.<p><pre><code> class T { public: T(int); }; T t; </code></pre> Will fail.
评论 #40888439 未加载
评论 #40887160 未加载
jeffbee11 个月前
I agree there is a lot of complexity in C++ in the year 2024, however I feel that much of the appearance of complexity around initialization is due to the pedantic, dogmatic use of the word &quot;default&quot; by the committee to mean &quot;not&quot;.
hwc11 个月前
I&#x27;m so glad I use Go more than C++ these days. In Go, all values are always zero-initialized if you don&#x27;t explicitly assign a value. If you need a constructor, you write a regular function that returns an explicitly assigned object.<p>I like keeping the rules of the language simple enough that there is never any confusion.
评论 #40883107 未加载
评论 #40883838 未加载
评论 #40884184 未加载
MathMonkeyMan11 个月前
Show me someone who understands the rules, and I&#x27;ll show you a compiler engineer... who probably doesn&#x27;t understand the rules.
sixtram11 个月前
Great link, I&#x27;m going to add this to my list of favorite interview questions. (^_-)
nxobject11 个月前
As an aside, I see that DEC front panel you&#x27;ve got there in your blog header.
vsgherzi11 个月前
this has got to be one of my favorite blog names i&#x27;ve seen on this site
gnarlouse11 个月前
this title makes me want to shout<p>&quot;Call J. G. Wentworth, 877 Cash Now!&quot;
3l3ktr411 个月前
This is the best title, OP.