
template<class _Ty> inline _Ty&& forward(typename remove_reference<_Ty>::type& _Arg) { // forward an lvalue return (static_cast<_Ty&&>(_Arg)); }template<class _Ty> inline _Ty&& forward(typename remove_reference<_Ty>::type&& _Arg) _NOEXCEPT { // forward anything static_assert(!is_lvalue_reference<_Ty>::value,"bad forward call"); return (static_cast<_Ty&&>(_Arg)); } 左值参考的一个版本,右值参考的一个版本.为什么不对rvalue和lvalue引用使用通用引用:
template <typename T,typename U>T&& Forward(U&& arg) { return static_cast<T&&>(arg);}解决方法 您的版本不符合标准,因为如果T是l值引用,则在rvalue上调用时,std :: forward是不需要编译的.来自[前进]: template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept;
2 Returns:
static_cast<T&&>(t).3 if the second form is instantiated with an lvalue reference type,the program is ill-formed.
以这种方式定义std :: forward以确保std :: forward的(某些)滥用不会编译.有关更多讨论,请参阅n2951(尽管即使n2951也不使用此确切形式).
总结以上是内存溢出为你收集整理的c – 使用通用引用实现转发功能全部内容,希望文章能够帮你解决c – 使用通用引用实现转发功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)