operator new 的执行顺序和构造函数的参数
Execution order of operator new and argument of constructor
C++ 规范是否指定 operator new
的顺序和 new C(A())
中 A
的构造函数。
g++ 的顺序是 A()
-> new
-> C()
,而 clang++ 的顺序是 new
-> A()
-> C()
.
差异是由未指定的行为引起的吗?
g++:7.4.0
铛++:10.0.0
#include <iostream>
#include <cstdlib>
struct A {
A() {
std::cout << "call A()\n";
}
};
struct C {
C(A) {
std::cout << "call S()\n";
}
void *operator new(size_t s) {
std::cout << "call new()\n";
return malloc(s);
}
};
int main() {
void *p = new C(A());
}
Clang 是正确的。从 C++17 开始,执行顺序是有保证的。 [expr.new]/19
The invocation of the allocation function is sequenced before the evaluations of expressions in the new-initializer.
operator new
(分配函数)应该首先被调用,然后在新初始化器(即A()
)中计算表达式。
在C++17之前,不保证顺序。 [expr.new]/18 (C++14)
The invocation of the allocation function is indeterminately sequenced with respect to the evaluations of expressions in the new-initializer.
gcc 似乎不符合 C++17(及更高版本);用 gcc10 in C++2a mode 编译给出相同的结果。
C++ 规范是否指定 operator new
的顺序和 new C(A())
中 A
的构造函数。
g++ 的顺序是 A()
-> new
-> C()
,而 clang++ 的顺序是 new
-> A()
-> C()
.
差异是由未指定的行为引起的吗?
g++:7.4.0 铛++:10.0.0
#include <iostream>
#include <cstdlib>
struct A {
A() {
std::cout << "call A()\n";
}
};
struct C {
C(A) {
std::cout << "call S()\n";
}
void *operator new(size_t s) {
std::cout << "call new()\n";
return malloc(s);
}
};
int main() {
void *p = new C(A());
}
Clang 是正确的。从 C++17 开始,执行顺序是有保证的。 [expr.new]/19
The invocation of the allocation function is sequenced before the evaluations of expressions in the new-initializer.
operator new
(分配函数)应该首先被调用,然后在新初始化器(即A()
)中计算表达式。
在C++17之前,不保证顺序。 [expr.new]/18 (C++14)
The invocation of the allocation function is indeterminately sequenced with respect to the evaluations of expressions in the new-initializer.
gcc 似乎不符合 C++17(及更高版本);用 gcc10 in C++2a mode 编译给出相同的结果。