What are the main purposes of using std::forward and which problems it solves?

后端 未结 6 2048
执念已碎
执念已碎 2020-11-21 07:12

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the

6条回答
  •  伪装坚强ぢ
    2020-11-21 07:38

    It may be worthwhile to emphasize that forward has to be used in tandem with an outer method with forwarding/universal reference. Using forward by itself as the following statements is allowed, but does no good other than causing confusion. The standard committee may want to disable such flexibility otherwise why don't we just use static_cast instead?

         std::forward(1);
         std::forward("Hello");
    

    In my opinion, move and forward are design patterns which are natural outcomes after r-value reference type is introduced. We should not name a method assuming it is correctly used unless incorrect usage is forbidden.

提交回复
热议问题