“ std :: forward”是否正确?和“ std :: move”不生成代码? [英] Is it true that "std::forward" and "std::move" do not generate code?

查看:84
本文介绍了“ std :: forward”是否正确?和“ std :: move”不生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: forward和 std :: move不生成代码是真的吗?我在<<有效的C ++ 11/14采样器>>。相关代码在脚注中。有人可以详细解释代码吗?对于这个问题,我将不胜感激。



根据文档( https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00416_source.html ),其中表示:

  / ** 
* @brief转发左值。
* @return将参数强制转换为指定的类型。
*
*此功能用于实现完美转发。
* /
template< typename _Tp>
constexpr _Tp&&
forward(typename std :: remove_reference< _Tp> :: type& __t)noexcept
{return static_cast< _Tp&&>(__ t); }



/ **
* @brief转发右值。
* @return将参数强制转换为指定的类型。
*
*此功能用于实现完美转发。
* /
template< typename _Tp>
constexpr _Tp&&
forward(typename std :: remove_reference< _Tp> :: type&& __t)noexcept
{
static_assert(!std :: is_lvalue_reference< _Tp> :: value,模板参数
替换_Tp是左值引用类型);
return static_cast< _Tp&>(__ t);
}
/ **
* @brief将值转换为右值。
* @param __t任意类型的事物。
* @return参数强制转换为右值引用以允许移动它。
* /
template< typename _Tp>
constexpr类型名称std :: remove_reference< _Tp> :: type&&
move(_Tp& __ t)noexcept
{返回static_cast< typename std :: remove_reference< _Tp> :: type&&>(__ t); }


解决方案

是否生成代码取决于编译器及其设置。如其他答案所示,如果禁用了优化,您可以期望会生成一些额外的代码



std :: move std :: forward 仅返回对该参数的引用,该参数在运行时不需要任何操作(值的更改类别会在编译时发生),并且如果启用了优化功能,则任何半好的像样的编译器都不会为它们生成代码。即使在调试版本中,也请使用 static_cast< T&> 代替这些功能。


Is it true that "std::forward" and "std::move" do not generate code? I saw this saying in << An Effective C++11/14 Sampler >>. The related code is at the footnote. Could somebody explain the code in detail? I would be very grateful to have some help with this question.

As per the documentation(https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00416_source.html), which says that:

   /**
    *  @brief  Forward an lvalue.
    *  @return The parameter cast to the specified type.
    *
    *  This function is used to implement "perfect forwarding".
    */
   template<typename _Tp>
     constexpr _Tp&&
     forward(typename std::remove_reference<_Tp>::type& __t) noexcept
     { return static_cast<_Tp&&>(__t); }



  /**
    *  @brief  Forward an rvalue.
    *  @return The parameter cast to the specified type.
    *
    *  This function is used to implement "perfect forwarding".
    */
   template<typename _Tp>
     constexpr _Tp&&
     forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
     {
       static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument"
                     " substituting _Tp is an lvalue reference type");
       return static_cast<_Tp&&>(__t);
     }
    /**
    *  @brief  Convert a value to an rvalue.
    *  @param  __t  A thing of arbitrary type.
    *  @return The parameter cast to an rvalue-reference to allow moving it.
   */
   template<typename _Tp>
     constexpr typename std::remove_reference<_Tp>::type&&
     move(_Tp&& __t) noexcept
     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }

解决方案

Whether something "generates code" or not depends on the compiler and its settings. As the other answer shows, you can expect some extra code to be generated if the optimizations are disabled.

std::move and std::forward merely return a reference to the parameter, which doens't require any actions at runtime (the change in value category happens at compile-time), and if optimizations are enabled, any half-decent compiler will generate no code for them.

If you want no extra code to be generated even in debug builds, use a static_cast<T &&> instead of those functions.

这篇关于“ std :: forward”是否正确?和“ std :: move”不生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆