gpt4 book ai didi

c++ - 编译器优化还是我的误区

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:25 26 4
gpt4 key购买 nike

最近我在测试一些C++的深暗角落,我对一个微妙的地方感到困惑。我的测试其实很简单:

// problem 1
// no any constructor call, g++ acts as a function declaration to the (howmany())
// g++ turns (howmany()) into (howmany(*)())
howmany t(howmany());

// problem 2
// only one constructor call
howmany t = howmany();

我对上面的期望是;第一个 howmany() 构造函数调用将生成一个临时对象,然后编译器将使用该临时对象和复制构造函数来实例化 t。然而,编译器的输出让我很困惑,因为输出只显示了一个构造函数调用。我的 friend 向我提到了编译器按值传递优化,但我们对此并不确定。我想了解这里发生了什么?

问题 2 的输出如下。问题 1 完全超出了对象实例化的范围,因为编译器将其视为函数指针声明。

howmany()
~howmany()

我的测试类是:

class howmany {
public:
howmany() {
out << "howmany()" << endl;
}
howmany(int i) {
out << "howmany(i)" << endl;
}
howmany(const howmany& refhm) {
out << "howmany(howmany&)" << endl;
}
howmany& operator=(const howmany& refhm) {
out << "operator=" << endl;
}
~howmany() {
out << "~howmany()" << endl;
}
void print1() {
cout << "print1()" << endl;
}
void print2() {
cout << "print2()" << endl;
}
};

最佳答案

这是 most vexing parse这里:

howmany t( howmany() );

为了解决这个问题,您需要添加一组额外的括号:

howmany t( (howmany()) );
^ ^

clang 在这里非常有用并警告您:

warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
howmany t( howmany() );
^~~~~~~~~~~~~
main.cpp:31:12: note: add a pair of parentheses to declare a variable
howmany t( howmany() );
^
( )

解决此问题的另一种方法是使用C++11 uniform initialization语法:

howmany t{ howmany{} };
^ ^^ ^

更新

解决您添加到问题 draft standard 的部分 2在某些情况下允许省略复制/移动结构。我们可以从 12.8 Copying and moving class objects 段落 31 中看到这一点,它说:

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances

并包括以下项目符号:

when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

关于c++ - 编译器优化还是我的误区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20665104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com