有人可以解释关于异常的右值引用吗? [英] Can someone explain rvalue references with respect to exceptions?

查看:173
本文介绍了有人可以解释关于异常的右值引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个异常类:

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 )lvalue ref。编译器说这些情况已经由rvalue ref catch 子句处理,这是可以理解的,因为例外是 xvalue 和也许捕获xvalue的最好的方法是一个右值ref(如果我错了,请纠正我)。但是,有人可以解释上述异常创建情况下的完美转发吗?这是对的吗?即使它编译,是有意义的还是有用的?如果我使用的C ++库有一个移动构造函数实现其 std :: exception 这种用法是真正有意义的?我尝试在关于异常的右值引用上搜索文章和SO问题,找不到任何。

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.

推荐答案

处理对于左值和右值具有特殊规则。临时异常对象是一个左值,参见当前草案的15.1 / 3:

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:


throw-expression初始化一个临时对象,称为异常对象,其类型通过从throw的操作数的静态类型中移除任何顶级cv限定符并将类型从T的数组或函数返回T调整为指向T的指针来确定,或者指向函数返回T的指针。 临时值为左值,用于初始化匹配处理程序(15.3)中命名的变量。如果异常对象的类型是不完全类型或指向不是(可能是cv限定的)void的不完整类型的指针,则程序是不成形的。除了这些限制和15.3中提到的类型匹配的限制外,throw的操作数完全作为调用(5.2.2)中的函数参数或返回语句的操作数处理。

A throw-expression initializes a temporary object, called the exception object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw and adjusting the type from "array of T" or "function returning T" to "pointer to T" or "pointer to function returning T", respectively. The temporary is an lvalue and is used to initialize the variable named in the matching handler (15.3). If the type of the exception object would be an incomplete type or a pointer to an incomplete type other than (possibly cv-qualified) void the program is ill-formed. Except for these restrictions and the restrictions on type matching mentioned in 15.3, the operand of throw is treated exactly as a function argument in a call (5.2.2) or the operand of a return statement.

并且通过rvalue引用捕获也是非法的,请参见15.3 / 1:

And catching by rvalue reference is illegal, too, see 15.3/1:


处理程序中的异常声明描述了可以导致处理程序输入的异常类型。 异常声明不能表示不完整类型或右值引用类型。除了void *,const void *,volatile void *或const volatile void *之外,异常声明不能表示指针或对一个不完全类型的引用。

The exception-declaration in a handler describes the type(s) of exceptions that can cause that handler to be entered. The exception-declaration shall not denote an incomplete type or an rvalue reference type. The exception-declaration shall not denote a pointer or reference to an incomplete type, other than void*, const void*, volatile void*, or const volatile void*.

此外,你似乎不理解完美的转发。你的前进调用没有比移动更好。完美转发的想法是将参数的值类别编码为类型的一部分,并让模板参数扣除计算出来。

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:

void inner(const int&);  // #1 takes only lvalues or const rvalues
void inner(int&&);       // #2 takes non-const rvalues only

template<class T>
void outer(T && x) {
    inner(forward<T>(x));
}

int main() {
   int k = 23;
   outer(k);   // outer<T=int&> --> forward<int&> --> #1
   outer(k+2); // outer<T=int>  --> forward<int>  --> #2
}

根据参数的值类别,模板argumend扣除推导T要么是左值引用,要么是正常值类型。由于参考崩溃,T&在第一种情况下也是一个引用,在第二种情况下,引用是一个 rvalue 引用。如果你看到T&和T是可以推导出的模板参数,它基本上是捕捉一切。 std :: forward恢复原始值类别(在T中编码),所以我们可以完美地将参数转发到重载的内部函数并选择正确的。但这只是因为外部是一个模板,因为有一些特殊的规则来确定T相对于它的值类别。如果你使用没有模板/模板参数扣除的右值引用(比如#2),函数只接受右值。

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.

这篇关于有人可以解释关于异常的右值引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆