AIFeed

在 C++ 中,= default 和 {} 并不是同一种构造函数

Translation(model translation, not the source's own text)

文章标题:在 C++ 中,= default 和 {} 不是同一个构造函数

正文:

2026 年 11 月 2 日 · 3 分钟

你可以把空的默认构造函数写成 span() = default 或 span() {}。它对性能可能有显著影响。


        struct span {
  const char *p;
  int len;
  const char *end;
  span() = default; // or span() {}
  span(const char *s, int n) : p(s), len(n), end(s + n) {}
};
      

在其首次声明处写 span() = default 会使 span 成为平凡类型(trivial)。而 span() {} 是用户提供的构造函数,因此 span 不是平凡类型。

构造函数声明发生什么成员
span() = defaultspan s;什么都不做未初始化
span() = defaultspan s{}整个对象被清零,包括填充字节;构造函数不会运行零
span() {}span s;调用空构造函数;编译器可以删除该调用未初始化
span() {}span s{}调用空构造函数;对象不会被清零未初始化

一个例子

这个 span 由一个起始指针、一个长度和一个结束指针组成。一个 box 持有一个 span。表达式 box() 会对该 span 进行值初始化。循环在 i 达到 n 时赋值为一个值初始化的 box,否则赋值为一个长度为 i + 1 的 span:


        struct box {
  span s;
  box() : s() {}
  box(span in) : s(in) {}
};
void consume(box &);
void walk(const char *s, int n) {
  for (int i = 0;; i++) {
    box b;
    b = i == n ? box() : box(span(s, i + 1));
    if (i == n) { return; }
    consume(b);
  }
}
      

box() 这条路径只在循环退出时执行一次,并且那个 span 不会被传给 consume。

使用 span() {} 时,GCC 16(-O2 -march=x86-64)会直接把三个字段写到 consume 期望的位置:


        .L4:
    mov eax, ebx
    mov QWORD PTR [rsp+16], rbx
    mov rdi, rsp
    add rbx, 1
    sub eax, ebp
    mov QWORD PTR [rsp], rbp
    mov DWORD PTR [rsp+8], eax
    call    consume(box&)
    cmp rbx, r12
    jne .L4
      

使用 span() = default 时,GCC 16 会把结束指针 rbx 直接存入 box。起始指针和长度则要经过一个临时对象,然后该临时对象的前 16 个字节被复制回来。


        .L3:
    mov eax, ebx
    mov QWORD PTR [rsp+32], rbp
    mov rdi, rsp
    sub eax, ebp
    mov QWORD PTR [rsp+16], rbx
    add rbx, 1
    mov DWORD PTR [rsp+40], eax
    movdqa  xmm0, XMMWORD PTR [rsp+32]
    movaps  XMMWORD PTR [rsp], xmm0
    call    consume(box&)
    cmp rbx, r12
    jne .L3
      

我在 Intel Xeon Gold 6548N(Emerald Rapids)的一个核心上,使用 -O2 -march=x86-64,对 1 亿个 span 的五次运行取最好成绩计时。我的代码已公开。

编译器span() = defaultspan() {}
GCC 14.31.73 ns/span1.74 ns/span
GCC 15.27.75 ns/span1.73 ns/span
GCC 16.28.04 ns/span1.73 ns/span
clang 211.75 ns/span1.74 ns/span
使用 span() = default 时,GCC 15 和 GCC 16 每个 span 约需 8 ns。花括号构造函数保持在 1.7 ns 附近。
使用 span() = default 时,GCC 15 和 GCC 16 每个 span 约需 8 ns。花括号构造函数保持在 1.7 ns 附近。

在 GCC 15 和 GCC 16 上,= default 版本慢了四倍多。

Daniel Lemire, "In C++, = default and {} are not the same constructor," in Daniel Lemire's blog, November 2, 2026, https://lemire.me/blog/2026/11/02/default-and-braces-are-not-the-same-constructor/. [BibTeX]

发布于

Daniel Lemire

魁北克大学(TELUQ)的计算机科学教授。查看 Daniel Lemire 的所有文章

发表评论 取消回复

你也可以通过电子邮件订阅此博客(非商业、无广告、每周一封邮件)。

如何发布代码(C、C++、Java、Python 等):

用反引号把你的代码包起来,像这样:


        `int main() {
    return 0;
}`
      

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Hacker News
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗