我从使用 Clang++ 在不同 C++ 版本下运行的相同代码片段得到了不同的结果。当我用 C++17 编译代码时,编译器似乎自动调用了 RVO/NRVO,好奇这是一个错误还是不同的功能?
Apple clang 版本 11.0.0 (clang-1100.0.33.17)
使用以下命令在C++11下运行:
clang++ test.cc -fno-elide-constructors -std=c++11 -o test
结果:
Move Constructor
Move Constructor
100
Run Code Online (Sandbox Code Playgroud)
使用以下命令在C++17下运行:
clang++ test.cc -fno-elide-constructors -std=c++17 -o test
结果:
100
Run Code Online (Sandbox Code Playgroud)
代码(test.cc):
struct A {
A() = default;
A(int v) : p(new int(v)) {}
~A() { delete p; }
A(const A&) = delete;
A& operator=(const A&) = delete;
A(A&& rhs) noexcept : p(rhs.p) {
std::cout << "Move Constructor" << std::endl;
rhs.p = nullptr;
}
A& operator=(A&& rhs) noexcept {
std::cout << "Move Operator" << std::endl;
p = rhs.p;
rhs.p = nullptr;
return *this;
}
int getPV() const { return *p; }
private:
int* p;
};
A getTempA(int v) { return A(v); }
int main(int argc, char** argv) {
auto a = getTempA(100);
std::cout << a.getPV() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 C++17 中,语言定义发生了变化。在您问题中的代码中,没有 temp ,也没有什么可省略的。
prvalue 表达式可以被视为延迟实例化。该表达式auto a = getTempA(100);根据定义与 相同A a(100);。
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |