其他分享
首页 > 其他分享> > c – 有人可以解释有关异常的右值引用吗?

c – 有人可以解释有关异常的右值引用吗?

作者:互联网

让我说我有这个异常类:

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)左值引用捕获时.编译器说这些情况已经由rvalue ref catch子句处理,这是可以理解的,因为例外是xvalue,并且捕获xvalue的最好方法可能是rvalue ref(如果我错了,请纠正我).但有人可以在上面的异常创建案例中解释perfect forwarding吗?这是正确的吗?即使它编译,它是有意义的还是有用的?我使用的C库是否应该为其std :: exception实现一个移动构造函数,以使这种用法真正有意义?我尝试在关于异常的rvalue引用上搜索文章和SO问题,找不到任何.

解决方法:

实际上,异常处理对左值和右值有特殊规则.临时异常对象是左值,见当前草案的15.1 / 3:

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.

并且通过右值引用捕获也是非法的,请参阅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*.

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

基本上,完美转发依赖于模板参数推导和右值引用:

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
}

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

标签:forwarding,c,c11,exception,rvalue-reference
来源: https://codeday.me/bug/20191005/1856034.html