有人可以解释关于异常的右值引用吗?
可以说我有这个异常类:
struct MyException : public std::exception
{
MyException(const std::exception &exc) : std::exception(exc)
{
cout << "lval\n";
}
MyException(std::exception &&exc) : std::exception(std::forward<std::exception>(exc))
{
cout << "rval\n";
}
};
...
...
try
{
throw std::exception("Oh no!");
// above is rvalue since it's got no name, what if the throw is made as
// std::exception lvalExc("Oh wierd!");
// throw lvalExc;
// if the throw is made thus, how can it be caught by catch(std::exception &&exc)?
}
catch(std::exception &&rValRef)
{
cout << "rValRef!\n";
throw MyException(std::forward<std::exception>(rValRef));
}
当我尝试按值或 (const) 左值引用捕获时。编译器表示这些情况已经由右值引用 catch
子句处理,这是可以理解的,因为例外是 xvalue 也许捕获 xvalue 的最佳方法是右值引用(如果我错了,请纠正我)。但是有人可以解释一下上述异常创建情况下的完美转发吗?正确吗?即使它可以编译,它是否有意义或有用?我使用的 C++ 库是否应该为其 std::exception
实现移动构造函数,以使这种用法真正有意义?我尝试搜索有关异常的右值引用的文章和问题,但找不到任何问题。
Lets say I've this exception class:
struct MyException : public std::exception
{
MyException(const std::exception &exc) : std::exception(exc)
{
cout << "lval\n";
}
MyException(std::exception &&exc) : std::exception(std::forward<std::exception>(exc))
{
cout << "rval\n";
}
};
...
...
try
{
throw std::exception("Oh no!");
// above is rvalue since it's got no name, what if the throw is made as
// std::exception lvalExc("Oh wierd!");
// throw lvalExc;
// if the throw is made thus, how can it be caught by catch(std::exception &&exc)?
}
catch(std::exception &&rValRef)
{
cout << "rValRef!\n";
throw MyException(std::forward<std::exception>(rValRef));
}
When I tried to catch by value or by (const) lvalue ref. the compiler says these cases are already handled by the rvalue ref catch
clause, which is understandable, as an exception is an xvalue and perhaps the best way to catch an xvalue is an rvalue ref (correct me if I'm wrong). But can someone explain about the perfect forwarding in the above case of exception creation? Is it correct? Even though it compiles, is it meaningful or useful? Should the C++ library I use have a move constructor implemented for its std::exception
for this kind of usage to be truly meaningful? I tried searching for articles and SO questions on rvalue references with respect to exceptions, couldn't find any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,异常处理对于左值和右值有特殊的规则。临时异常对象是一个左值,参见当前草案的15.1/3:
通过右值引用捕获也是非法的,请参见 15.3/1:
另外,你似乎不太了解完美转发。你的前向调用并不比移动更好。完美转发的思想是将参数的值类别编码为类型的一部分,并让模板参数推导来计算它。但是您的异常处理程序不是也不可能是函数模板。
基本上,完美转发依赖于模板参数推导和右值引用:
根据参数的值类别,模板参数推导将 T 推导为左值引用或普通值类型。由于参考崩溃,T&&在第一种情况下也是左值引用,在第二种情况下也是右值引用。如果您看到 T&& T 是一个可以推导的模板参数,它基本上是“捕获一切”。 std::forward 恢复原始值类别(以 T 编码),因此我们可以完美地将参数转发到重载的内部函数并选择正确的函数。但这仅起作用,因为外部是模板,并且有特殊规则来确定 T 的值类别。如果您使用没有模板/模板参数推导的右值引用(如#2),则该函数将仅接受右值。
Actually, exception handling has special rules with respect to lvalues and rvalues. The temporary exception object is an lvalue, see 15.1/3 of the current draft:
And catching by rvalue reference is illegal, too, see 15.3/1:
Also, you don't seem to understand perfect forwarding. Your forward invocation is no better than a move. The idea of perfect forwarding is to encode the value category of the argument as part of the type and let template argument deduction figure it out. But your exception handler is not and cannot be a function template.
Basically, perfect forwarding relies on template argument deduction and rvalue references:
Depending on the value category of the argument, template argumend deduction deduces T to be either an lvalue reference or a normal value type. Due to reference collapsing, T&& is also an lvalue reference in the first case, or an rvalue reference in the second case. If you see T&& and T is a template parameter which can be deduced, it's basically a "catch everything". std::forward restores the original value category (encoded in T) so we can perfectly forward the argument to the overloaded inner functions and select the correct one. But this only works because outer is a template and because there are special rules for determining T with respect to its value category. If you use an rvalue references without templates/template argument deduction (like in #2), the function will only accept rvalues.