c++ - The implementation of std::forward -
i'm reading overview of new c++ (c++11/14) (pdf only), @ slide 288 gives implementation of std::forward
:
template<typename t> // lvalues (t t&), t&& std::forward(t&& param) // take/return lvalue refs. { // rvalues (t t), return static_cast<t&&>(param); // take/return rvalue refs. }
and gives implemention in text:
the usual std::forward implementation is:
template<typename t> struct identity { typedef t type; }; template<typename t> t&& forward(typename identity<t>::type&& param) { return static_cast<identity<t>::type&&>(param); }
what difference? why latter usual implementation?
the problem first can write std::forward(x)
, doesn't want, since produces lvalue references.
the argument in second case non-deduced context, preventing automatic deduction of template argument. forces write std::forward<t>(x)
, right thing do.
also, argument type second overload should typename identity<t>::type&
because input idiomatic use of std::forward
lvalue.
edit: standard mandates signature equivalent 1 (which, incidentally, libc++ has):
template <class t> t&& forward(typename remove_reference<t>::type& t) noexcept; template <class t> t&& forward(typename remove_reference<t>::type&& t) noexcept;
Comments
Post a Comment