gpt4 book ai didi

c++ - 为什么在以下示例中创建临时文件

转载 作者:行者123 更新时间:2023-11-28 02:23:11 25 4
gpt4 key购买 nike

能否请您解释一下(在标准方面更好)为什么在以下示例中在注释标记的行中创建临时文件?

class A
{
public:

bool operator<(const A& rhs) const {return true;}

};

set<A> s;

int main()
{
s.insert(A());
set<A>::iterator pos;
pos = s.begin();
(A)*pos; // why temporary created here?

return 0;
}

最佳答案

每次转换为对象类型的结果都是临时的。

当转换是身份转换时,请注意 Microsoft Visual C++ 不符合此规则。我已举报 a bug on Microsoft Connect它还显示了标准中需要创建临时文件的规则:

The result of the expression (T) cast-expression is of type T. The result is an lvalue if T is an lvalue reference type or an rvalue reference to function type and an xvalue if T is an rvalue reference to object type; otherwise the result is a prvalue.

(标准的第 5.4 节 [expr.cast]xvalue 语言是在 C++11 中添加的,但在这种情况下创建临时对象总是存在)

关于c++ - 为什么在以下示例中创建临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31574561/

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