显式删除默认构造函数的目的

发布于 2025-01-19 03:18:13 字数 352 浏览 2 评论 0原文

我正在开发的代码库主要是在 C++11 之前开发的。许多类在私有部分中声明了从未定义的默认构造函数。我相当有信心,在现代 C++ 中,正确的方式™ 是将它们公开并=删除 它们。到目前为止,我已经将课程“升级”了无数次,但从来没有出现过问题。

我的问题是:为什么要这样做? 这个答案说,只有在用户没有给出构造函数的情况下才会提供默认构造函数(我猜这不包括 < code>= default),并且没有任何迹象表明它不适用于 C++11 之前的版本。当然,我正在谈论的所有类中都有一个不平凡的构造函数。那么,我缺少什么理由吗?

The codebase I’m working on was developed mostly pre-C++11. A lot of classes have a never-defined default constructor declared in the private section. I’m rather confident that in Modern C++, the Correct Way™ is to make them public and = delete them. I “upgraded” classes to this countless times by now and it never lead to problems.

My question is rather: Why was that done at all? This answer said that a default constructor is only ever provided if there’s no constructor given by the user (I guess that’s not including = default) and there’s no hint that it doesn’t apply to pre-C++11. Of course, there is a non-trivial constructor in all of my classes I’m talking about. So, is there a rationale for it that I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

儭儭莪哋寶赑 2025-01-26 03:18:14

任何函数都可以=删除。默认构造函数是一个函数,因此可以删除它。没有必要为此做出语言限制。

当编译器不会生成默认构造函数(或没有定义的私有声明的 C++ 之前的伪等效项)时,某些用户选择显式删除它是无害的。事实上,它有一些小好处。

  1. 如果有人更改类以删除其构造函数,该类不会突然获得默认构造函数。

  2. 生成默认构造函数时,您不必记住规则是什么。例如:

    <块引用>

    我猜这不包括 = default

    这证明了我的观点,因为你猜错了。显式默认构造函数确实算作“用户提供的”,因此它们确实抑制了隐式默认构造函数的创建。必须记住这一点很麻烦;直接说出来会更清楚。

Any function can be = deleted. A default constructor is a function, so it can be deleted. There's no need to make a language carveout for that.

That some users choose to explicitly delete the default constructor (or the pre-C++ pseudo-equivalent of a private declaration with no definition) when it would not have been generated by the compiler is harmless. Indeed, it has some small benefits.

  1. If someone changes the class to remove its constructors, the class won't suddenly gain a default constructor.

  2. You don't have to remember what the rules are about when a default constructor is generated. For example:

    I guess that’s not including = default

    This proves my point, because you guessed wrong. Explicitly defaulted constructors do count as "user-provided", and thus they do suppress the creation of an implicit default constructor. Having to remember that is bothersome; it's clearer to just state it outright.

看海 2025-01-26 03:18:14

正如该问题下的评论中清楚的那样,声明了构造函数(或成员函数),但从未实现的是不良设计,导致错误消息。 = delete无论如何都不会生成的东西的唯一合理原因是记录明确的意图。就我而言,这些班级都没有给它具有默认约束的印象。

我仍然不确定在我的情况下是什么理想的,因为这取决于谁将在侵犯侵蚀的代码基础上工作。目前,我将继续将私有的永无止境更改为= delete,但请留下。很容易找到= delete构造函数,并在将它们删除后恢复它们而不替换。

As became clear in the comments under the question, having a constructor (or a member function) declared but never implemented is bad design that leads to bad error messages. The only justified reason to = delete something that would not have been generated anyways, is documenting explicit intent. In my case, none of the classes gives the impression that it should have a default construtor.

I’m still not sure what’s ideal in my case because that depends on who’s going to work on the code base in the furute. For now, I’ll continue to change private never-defiend to = delete, but leave it at that. It’s rather easy to find = delete constructors and replace them with nothing compared to restoring them after they’re removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文