有人可以在例外情况下解释rvalue引用吗? [英] Can someone explain rvalue references with respect to exceptions?

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

问题描述

让我们说我有这个异常类:

  struct MyException:public std :: exception 
{
MyException(const std :: exception& exc):std :: exception(exc)
{
cout<< lval\\\
;
}
MyException(std :: exception&& exc):std :: exception(std :: forward< std :: exception>(exc))
{
cout< rval\\\
;
}
};

...
...

尝试
{
throw std :: exception(哦不!
//以上是rvalue,因为它没有名字,如果抛出是
// std :: exception lvalExc(哦wierd!);
// throw lvalExc;
//如果抛出,那么如何捕获catch(std :: exception&& exc)?
}
catch(std :: exception&& rValRef)
{
cout<< !rValRef \\\
;
throw MyException(std :: forward< std :: exception>(rValRef));
}

当我尝试通过值或通过( em>)lvalue ref。编译器说这些情况已经由rvalue ref catch 子句处理,这是可以理解的,因为例外是一个 xvalue ,也许抓住xvalue的最佳方式是rvalue ref(如果我错了,请纠正我)。但有人可以在上述异常创建的情况下解释完美转发吗?这是对的吗?即使它编译,它是有意义还是有用的?如果我使用的C ++库具有为其 std :: exception 实现的移动构造函数,以使这种用法真正有意义?

解决方案

实际上,例外情况下,我尝试搜索关于异常的rvalue引用的文章和SO问题。处理有关于左值和右值的特殊规则。临时异常对象是一个左值,见当前草案的15.1 / 3:


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


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


处理程序中的异常声明描述了可能导致处理程序被输入的异常的类型。 异常声明不应表示不完整的类型或rvalue引用类型。除了void *,const void *,volatile void *或const volatile void *之外,异常声明不应表示为不完整类型的指针或引用。


此外,你似乎不了解完美的转发。你的向前调用并不比移动更好。完美转发的想法是将参数的值类别编码为类型的一部分,并让模板参数推导计算出来。但是你的异常处理程序不是而且不能是一个函数模板。



完全转发依赖于模板参数扣除和rvalue引用:

  void inner(const int&); //#1只需要lvalues或const rvalue 
void inner(int&&); //#2仅使用非常量rvalue

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

int main(){
int k = 23;
outer(k); // outer< T = int&> - >前向和LT; INT&安培;> - > #1
outer(k + 2); // outer< T = int> - >前向和LT;诠释> - > #2
}

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


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.

解决方案

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:

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.

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

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
}

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.

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

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