Kann jemand rWertreferenzen in Bezug auf Ausnahmen erklären?
Sagen wir, ich habe diese Ausnahmeklasse:
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));
}
Wenn ich versucht habe, nach Wert oder nach const) lWert ref. Der Compiler sagt, dass diese Fälle bereits vom rvalue ref @ behandelt werdecatch
-Klausel, die verständlich ist, da eine Ausnahme ein @ i xvalue und vielleicht ist der beste Weg, einen x-Wert zu fangen, ein r-Wert ref (korrigiere mich, wenn ich falsch liege). Kann mir aber jemand etwas über das @ erklärperfekte Weiterleitung im obigen Fall der Ausnahmeerstellung? Ist es richtig? Auch wenn es kompiliert wird, ist es sinnvoll oder nützlich? Sollte die von mir verwendete C ++ - Bibliothek einen Verschiebungskonstruktor für sein @ habstd::exception
Damit diese Art der Verwendung wirklich aussagekräftig ist? Ich habe versucht, nach Artikeln und SO-Fragen zu Wertverweisen in Bezug auf Ausnahmen zu suchen, konnte jedoch keine finden.