【发布时间】:2019-11-20 19:15:55
【问题描述】:
考虑以下 C++ 代码:
#include <iostream>
#include <string>
#define DUMP(_, str) do { \
std::cout << str; \
for (int i = 0; i < 10; ++i) \
std::cout << (_).x[i] << std::endl; \
std::cout << "a = " << (_).a << std::endl; \
std::cout << "b = " << (_).b << std::endl; \
std::cout << "c = " << (_).c << std::endl; \
std::cout << "d = " << (_).d << std::endl; \
std::cout << std::endl; \
} while (0)
//#define ENABLE_WTF
struct A {
int x[10];
float a, b, c, d;
#ifndef ENABLE_WTF
A() : d(4) { DUMP(*this, "=== INSIDE CTOR ===\n"); }
#else
A() : d(4) {}
#endif
};
int main() {
A a;
DUMP(a, "=== OUT OF CTOR ===\n");
}
可以看出,A 有一个部分构造函数,它只初始化一个字段,而所有其他字段可以预见地仍然是垃圾; demonstration here.
现在的问题是:当它的部分构造函数没有主体 (demonstration here) 时,它是编译器特定的,以将 A 的其余部分归零,还是它是 C++ 本身的一部分?
【问题讨论】:
-
无法重现:godbolt.org/z/ysUqYG
-
我用不同的编译器和命令行选项多次注释/取消注释它,但从未看到全零。
-
@Evg Hm,所以看起来我目前使用的编译器(ICPC 19.0)恰好与运行 cpp.sh 的编译器类似。过失,我没有考虑过这种可能性,因此错误地认为这种行为是普遍的。
标签: c++ constructor language-lawyer raii