Max
Max

Reputation: 880

Perfect forwarding c++

Is this correct way to use 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))

Upvotes: 0

Views: 127

Answers (1)

eerorika
eerorika

Reputation: 238331

Is this correct way to use std::forward?

No, there's no point to use std::forward here. 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:

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

Upvotes: 5

Related Questions