开发者生态
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必读
每日精选科技资讯,直达你的邮箱
相关推荐
Jovial J73 编译器
A fully functional native compiler for JOVIAL J73 (MIL-STD-1589C), a programming
👁 0
★ 100.0
显示 HN:GoModel – 开源 AI 网关 – LiteLLM 替代方案
Install and run GoModelRun it with Docker - standalone or as the full local stac
👁 0
★ 100.0
Show HN:自托管 Docker 镜像注册表。一个容器,两个简单账户
One container, one port, one data folder. Back up ./data and keep your deploymen
👁 0
★ 100.0
蜜罐可以攻击黑客代理
Honeypots that hack the hacking agent back. ARTEX is an autonomous AI pentest ag
👁 0
★ 100.0
GitHub 热门项目:z80ai
GitHub项目:z80ai 仓库地址:https://github.com/HarryR/z80ai Stars:1143 | 作者:HarryR 项目描述:
👁 0
★ 100.0