有一個現有的expected<T,E>類提供這些typedefs和operators:
value_type = T
operator *(): expected<T,E>& -> value_type&
const expected<T,E>& -> const value_type&
expected<T,E>&& -> value_type&&
const expected<T,E>&& -> const value_type&&
現在我正在撰寫這樣的函式:
template <typename E>
/*type*/ Unwrap(E&& e)
{
return e.has_value() ? /*what*/
: throw e.error();
}
我應該在評論區放什么?
我試過auto&&和*e,它收到了excepted&&,但回傳的value_type&。
我也試過std::forward,但它甚至無法編譯。
我應該怎么做?
uj5u.com熱心網友回復:
您可以使用decltype(auto)作為回傳型別:
#include <utility>
template<typename E>
decltype(auto) Unwrap(E&& e) {
return e.has_value() ? *std::forward<E>(e)
: throw e.error();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/397264.html
上一篇:在模板中使用不完整的模板型別
下一篇:FlutterDoctorlicensestatusissueAndroidtoolchain-developforAndroiddevices(AndroidSDKversion32.0.0)
