What is the purpose of std::forward()'s rvalue reference overload?

前端 未结 1 589
北海茫月
北海茫月 2021-01-17 09:32

I\'m experimenting with Perfect Forwarding and I found that std::forward() needs two overloads:

Overload nr. 1:

template 
inline T         


        
相关标签:
1条回答
  • 2021-01-17 10:23

    The design rationale for forward is discussed in great detail in N2951.

    This document lays out 6 use cases:

    A. Should forward an lvalue as an lvalue. All implementations pass this test. But this is not the classic perfect forwarding pattern. The purpose of this test is to show that implementation 2 fails in its stated goal of preventing all use cases except perfect forwarding.

    B. Should forward an rvalue as an rvalue. Like use case A, this is an identity transformation and this presents a motivating example where the identity transformation is needed.

    C. Should not forward an rvalue as an lvalue. This use case demonstrates a dangerous situation of accidentally creating a dangling reference.

    D. Should forward less cv-qualified expressions to more cv-qualified expressions. A motivating use case involving the addition of const during the forward.

    E. Should forward expressions of derived type to an accessible, unambiguous base type. A motivating use case involving forwarding a derived type to a base type.

    F. Should not forward arbitrary type conversions. This use case demonstrates how arbitrary conversions within a forward lead to dangling reference run time errors.

    The second overload enables cases B and C.

    The paper goes on to provide examples of each use case, which are too lengthy to be repeated here.

    Update

    I've just run the "solution" of just the first overload through these 6 use cases, and this exercise shows that the second overload also enables use case F: Should not forward arbitrary type conversions.

    0 讨论(0)
提交回复
热议问题