gpt4 book ai didi

c++ - 复制构造函数和抛出表达式

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

我有一个关于抛出表达式和复制构造函数的问题。

#include <iostream>

class Foo
{
public:
Foo() { std::cout << "Foo::Foo() \n"; }
Foo(const Foo&) { std::cout << "Foo::Foo(const Foo&) \n"; }
~Foo() { std::cout << "Foo::~Foo() \n"; }
};

int main()
{
try
{
throw Foo();
}
catch (...)
{
std::cerr << "Error \n";
}
}

在 C++98/C++03 中,这段代码是否打印:

Foo::Foo()
Error
Foo::Foo(const Foo&)
Foo::~Foo()

并且在 C++11 中是以下之一:

Foo::Foo()
Error
Foo::Foo(const Foo&)
Foo::~Foo()

Foo::Foo()    
Error
Foo::~Foo()

我只在 C++11 中看到过这个:

12.8 Copying and moving class objects [class.copy]

31 ... — in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object

最佳答案

未定义异常对象的复制(或在 C++11 中移动),但它必须是可复制的(或在 C++11 中可移动)并且实现可以复制(或移动)它的次数不限他们喜欢搞笑。

关于c++ - 复制构造函数和抛出表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12356332/

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