问题描述
我看到很多不同的地方,统一初始化是。 Herb Sutter 它,并在 not 给出一个列表用它。似乎人们普遍同意使用这种语法。
I see a lot of different places that uniform initialization is recommended. Herb Sutter recommends it, and gives a list when not to use it. It seems that the general consensus is to use this syntax.
但是,我不知道为什么。它具有 std :: initializer_list
优先的问题。将 std :: initializer_list
添加到类中可能会破坏代码。对于模板,不建议使用。与旧方式相比,它似乎有更多的例外。这些问题都不是旧方法存在的。
However, I don't see why. It has the problem of std::initializer_list
takes precedence. Adding a std::initializer_list
to a class can break code. With templates, it is not recommended to use. It seems to have more exceptions than the "old" way. None of these problems existed with the old way.
我看不出为什么统一初始化更好。我的结论是继续使用()
语法,并在 中使用 {}
我想用 std :: initializer_list
调用构造函数的情况。
I fail to see why uniform initialization is superior. My conclusion is to keep using ()
syntax, and use {}
only in the case of when I want to call a constructor with std::initializer_list
.
为什么?统一初始化有什么作用?
Why? What does uniform initialization give?
- 禁止缩小:好的功能。但是,由于我为我的代码全部启用了缩小警告(因为我想知道代码中的所有缩小范围,而不仅仅是在初始化时),因此我不需要太多此功能。
- 最烦人的解析:是的,这是一个问题,但是我很少碰到这个问题。因此,这不是(对我而言)切换的理由。在这里,我可能会使用
{}
。 - 还有其他内容吗(也许我还在学习C ++的新功能) ?
- forbids narrowing: good feature. But, as I have narrowing warnings turn on for all my code (because I want to know all narrowings in my code, not just at initializations), I don't need this feature too much.
- most vexing parse: yeah, that's a problem, but I hit this very-very rarely. So it is not a reason (for me) to switch. This places, I may use
{}
. - is there anything else (maybe, I'm still learning new features of C++)?
采用旧方式,无需记住任何规则,也不会破坏代码。只需使用它,有时(很少)会遇到最令人头疼的问题。就是这样。
With the "old" way, there were no rules to remember, no possible break-of-code. Just use it, and sometimes, very-very rarely, you hit the most vexing parse. That's all.
我的想法在哪里不正确?
Is my thinking wrong somewhere?
推荐答案
它似乎您在技术方面拥有不错的实力,而 nwp
提出了我要在评论中提及的其他问题。因此,我认为您拥有做出决定所需的信息。
It seems like you have a decent hold on the technical aspects and nwp
raised the other concern I would mention, clarity, in the comments. So I think you have the information you need to make a decision.
话虽如此,我认为值得加倍,并试图强调清晰度方面的重要性。以我的经验,代码清晰可能是在代码库中维护的最重要的事情。特别是在避免混乱和限制时间上浪费在愚蠢的bug上。我认为我们所有人都有花费太长时间来调整一段错误代码流的经验,直到最终发现该问题是最初意图的错别字或误解。
That said, I think it's worth doubling-down and trying to highlight the importance of the clarity aspect. In my experience, code clarity is probably the single most importance thing to maintain in a code base. Particularly in terms of avoiding confusion and limiting time wasted on stupid bugs. I think we've all had the experience of spending far too long tweaking the flow of a piece of buggy code only to eventually discover that the issue was a typo or misunderstanding of the original intent.
为了公平起见,这听起来像是您试图通过坚持一种有助于解决问题的样式和工具来解决此问题。从 nwp
开始的标准论点是,许多人不习惯使用该语言内置的命名约定或使用IDE。我个人对此逻辑表示同情,但也理解为什么许多人不理会它是过时的甚至是非问题(特别是对于IDE而言)。
And to be fair, it sounds like you've tried to address this by sticking to a style and tools that help address that. The standard argument, as nwp
started, is that many people aren't okay with deviating from the naming conventions built into the language, nor with using IDEs. I personally sympathize with that logic but also understand why many disregard it as old-fashioned or even a non-issue (particularly for the case of IDEs).
不过,为了清楚起见,我发现很难忘记,即使在可能会帮助他们的情况下,人们也会讨厌忽略较小的细节。因此,越多的上下文线索可以暗示某个问题可能更好。语法高亮显示很棒,但我不会押注调试一个晚上,因为它能够注意到黄色而不是橙色。黄色并使用骆驼保护套?也许。黄色并使用花括号?
When it comes to matters of clarity, though, I find it hard not to keep in mind that people are annoyingly good at ignoring minor details, even when they might help them. So the more context clues that can hint at where an issue might be the better. Syntax highlighting is great, but I wouldn't bet an evening of debugging on being able to notice something being yellow instead of orange. Yellow and using camel-case? Maybe. Yellow and using braces? Maybe.
特别是当您开始使用别人编写的代码时,或者很久以前,所有这些小提示就变得越来越重要。
Especially when you start getting into code written by someone else, or a long time ago, the more all these little hints start to matter.
最后,我认为这就是为什么人们喜欢它以至于推荐它的原因。当事情变得重要时,这种事情可能会一直持续下去。
At the end of the day, I think that's why people like it enough to recommend it. It's the kind of thing that might just stick out to you when it matters.
您对收窄的评论。启用警告来缩小范围可能会让您暂时忽略此好处,但是在许多情况下,人们1)由于遗留代码而无法启用此类警告,或者2)不想启用此类警告,因为他们有意依赖于此类行为在某些情况下,会将警告视为令人讨厌的事情。但是,无论哪种情况,都采用列表初始化不仅可以避免潜在的问题,而且可以明确指定代码行的意图。
Also, a side note in response to your comment about narrowing. Enabling warnings for narrowing may allow you to ignore this benefit for now but in many cases people either 1) can't enable such warnings due to legacy code or 2) don't want to enable such warnings because they intentionally rely on such behavior in certain circumstances and would consider the warnings a nuisance. However, adopting list initialization in either case could help with not only preventing potential issues but also making clear the intent of a given line of code.
人们都有不同的情况和偏好。诸如此类的功能增加了人们改善代码质量/可读性的方式,同时仍可在他们可能遇到的任何限制(自我施加或其他限制)下工作。
To get to the point, people all have different circumstances and preferences. Features like this increase of ways people can improve their code quality/readability while still working within whatever constraints they may have (self-imposed or otherwise).
这篇关于为什么建议统一初始化(使用花括号初始化)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!