开发者生态
morning
在C++中,=default和{}不是同一个构造函数
摘要
【HN用户评论摘要】 Constructors are such an odd duck to me. On the one hand, you have languages like C++ and Swift which have construction as a first-class concept, with tons of rules you need to memorize (Sw...
the
you
span
are
have
default
like
and
which
that
2026-11-26
1 阅读
约2分钟阅读
surprisetalk
字号:
【HN用户评论摘要】
构造函数对我来说是个奇怪的鸭子。一方面,你有像 C++ 和 Swift 这样的语言,它们将构造作为一流的概念,有大量的规则你需要记住(Swift 的“方便”与“指定”初始化器是解压的真正享受)。然后你有像 Rust、Go、Zig 等根本没有它们的语言,构造函数只是类型名称空间中的静态函数,它返回该类型的实例。我一生都不明白为什么你'
没有一种语言像 C++ 那样让我害怕。看这个页面:https://www.learncpp.com/cpp-tutorial/variable-assignment-an...这只是初学者教程第一章的一个部分。而且它需要记住的东西已经比整个 Lua 还要多了。
该示例非常人为,因为实际上您无论如何都希望对默认构造函数中的成员进行零初始化。 结构跨度{
const char *p = nullptr;
size_t 长度 = 0;
跨度()=默认值; // 或 span() {}
span(const char *s, size_t n) : p(s), len(n) {}
};
在这种情况下,span 之间没有区别;
和跨度 s{};
只有少数用例需要不初始化其成员的默认构造函数。一个例子
强制性模因https://imgur.com/forest-gump-c-3wlxtI0
这对 LLVM 没有影响
原始链接:https://lemire.me/blog/2026/11/02/default-and-braces-are-not-the-same-constructor/
这篇文章对您有帮助吗?
订阅66必读
每日精选科技资讯,直达你的邮箱
相关推荐
Show HN:Flide:使类似 Jev 的决策模型速度提高 9.5 倍
English · ç®€ä½“ä¸æ–‡ · 日本語 News · Quick start · Models · Options Fl
👁 0
★ 100.0
精益魔法书
Back in July, I started experimenting with using modern coding agents to build a
👁 0
★ 100.0
Fast-Anything:不要用 Rust 重写它。重写缓慢的 5%
Don't rewrite it in Rust. Rewrite the 5% that's slow. Rewriting a whole app in R
👁 0
★ 100.0
GitHub 热门项目: secure-openclaw
A personal 24x7 AI assistant that runs on your messaging platforms. Send a messa
👁 0
★ 100.0
《GTA6》代步车奶奶演员回应:笑称自己已成表情包
《GTA6》代步车奶奶演员回应:笑称自己已成表情包 2026-11-26 19:42:56 出处:游民星空 作者:LHertZ 编辑:拾柒 评论()复制纠错 随
👁 0
★ 100.0