简体   繁体   English

“std::forward”和“std::move”真的不会生成代码吗?

[英]Is it true that “std::forward” and “std::move” do not generate code?

Is it true that "std::forward" and "std::move" do not generate code? “std::forward”和“std::move”确实不会生成代码吗? I saw this saying in << An Effective C++11/14 Sampler >>.我在<< 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:根据文档( https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00416_source.html ),其中说:

   /**
    *  @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. std::movestd::forward仅返回对参数的引用,这在运行时不需要任何操作(值类别的变化发生在编译时),如果启用了优化,任何半体面的编译器不会为他们生成任何代码。

If you want no extra code to be generated even in debug builds, use a static_cast<T &&> instead of those functions.如果即使在调试版本中也不希望生成额外的代码,请使用static_cast<T &&>而不是这些函数。

That's not true.这不是真的。 It generates code.它生成代码。 The code编码

#include <utility>

int main() {
    int a;
    int b = std::move(a);
}

generates this assembly with Clang 10.0 (without optimization):使用 Clang 10.0(未优化)生成此程序集:

main:                                   # @main
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        lea     rdi, [rbp - 4]
        call    std::remove_reference<int&>::type&& std::move<int&>(int&)
        xor     ecx, ecx
        mov     edx, dword ptr [rax]
        mov     dword ptr [rbp - 8], edx
        mov     eax, ecx
        add     rsp, 16
        pop     rbp
        ret
std::remove_reference<int&>::type&& std::move<int&>(int&): # @std::remove_reference<int&>::type&& std::move<int&>(int&)
        push    rbp
        mov     rbp, rsp
        mov     qword ptr [rbp - 8], rdi
        mov     rax, qword ptr [rbp - 8]
        pop     rbp
        ret

and the code和代码

#include <utility>

int main() {
    int a;
    int b = a;
}

generates this assembly with Clang 10.0 (without optimization):使用 Clang 10.0(未优化)生成此程序集:

main:                                   # @main
        push    rbp
        mov     rbp, rsp
        xor     eax, eax
        mov     ecx, dword ptr [rbp - 4]
        mov     dword ptr [rbp - 8], ecx
        pop     rbp
        ret

https://godbolt.org/z/DthcYe https://godbolt.org/z/DthcYe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM