简体   繁体   English

完美转发 c++

[英]Perfect forwarding c++

Is this correct way to use std::forward ?这是使用std::forward的正确方法吗?

struct Command {
public:
  int32_t uniqueID{0};

public:
  Command() = default;
  
  template<typename T>
  Command(T&& _uniqueID) : uniqueID(std::forward<int32_t>(_uniqueID)) //
  {
    //
  }
};

Or I should use this line?或者我应该使用这条线?

Command(T&& _uniqueID) : uniqueID(std::forward<T>(_uniqueID))

Is this correct way to use std::forward?这是使用 std::forward 的正确方法吗?

No, there's no point to use std::forward here.不,这里没有必要使用std::forward Moving a primitive type is same as copying it.移动原始类型与复制它相同。 I recommend following:我建议如下:

Command(std::int32_t _uniqueID) : uniqueID(_uniqueID) {}

Or alternatively, let the class be an aggregate:或者,让 class 成为一个聚合:

struct Command {
    std::int32_t uniqueID{0};
};

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

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