本文介绍了有人可以在例外情况下解释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的最佳方式是rvalue ref(如果我错了,请纠正我)。但有人可以在上述异常创建的情况下解释吗?这是对的吗?即使它编译,它是有意义还是有用的?如果我使用的C ++库具有为其 std :: exception 实现的移动构造函数,以使这种用法真正有意义?

解决方案

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

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

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



完全转发依赖于模板参数扣除和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:

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:

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引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:57