我期望std :: pair和std :: tuple具有类似的行为。但事实证明,与std :: pair相比,std :: tuple生成更差的asm代码。
https://gcc.godbolt.org/z/Ri4M8z-gcc 10.0.0每晚构建,-O3 -std=c++17
针对x86-64 System V ABI进行编译
#include <utility>
std::pair<long, long> test_pair() {
return { 1, 2 };
}
Run Code Online (Sandbox Code Playgroud)
# returned in RDX:RAX
test_pair():
mov eax, 1
mov edx, 2
ret
Run Code Online (Sandbox Code Playgroud)
#include <tuple>
std::tuple<long, long> test_tuple() {
return { 1, 2 };
}
Run Code Online (Sandbox Code Playgroud)
# returned via hidden pointer, not packed into registers
test_tuple():
mov QWORD PTR [rdi], 2
mov QWORD PTR [rdi+8], 1
mov rax, rdi
ret
Run Code Online (Sandbox Code Playgroud)
这两个函数都返回两个值。test_pair使用寄存器存储期望值;但是test_tuple将值存储在堆栈中,这似乎是未优化的。为什么这两个函数的行为不同?
class A {
#a = 1;
static #a = 2;
}
Run Code Online (Sandbox Code Playgroud)
结果是
Uncaught SyntaxError: redeclaration of private name #a在火狐浏览器中Uncaught SyntaxError: Identifier '#a' has already been declared在 Chrome 中尽管
class A {
a = 1;
static a = 2;
}
Run Code Online (Sandbox Code Playgroud)
在 Firefox 和 Chrome 中均有效
AFAIK 实例字段将安装在类实例上,而静态字段将安装在类对象本身上。他们并不冲突。为什么之前的代码无效?
abi ×1
assembly ×1
c++ ×1
class ×1
class-fields ×1
javascript ×1
private ×1
static ×1
x86-64 ×1