这是正确的使用方法吗std::forward?
struct Command {
public:
int32_t uniqueID{0};
public:
Command() = default;
template<typename T>
Command(T&& _uniqueID) : uniqueID(std::forward<int32_t>(_uniqueID)) //
{
//
}
};
Run Code Online (Sandbox Code Playgroud)
或者我应该使用这条线?
Command(T&& _uniqueID) : uniqueID(std::forward<T>(_uniqueID))
Run Code Online (Sandbox Code Playgroud)
这是使用 std::forward 的正确方法吗?
不,这里没有任何用处std::forward。移动基本类型与复制它相同。我建议如下:
Command(std::int32_t _uniqueID) : uniqueID(_uniqueID) {}
Run Code Online (Sandbox Code Playgroud)
或者,让类成为一个聚合:
struct Command {
std::int32_t uniqueID{0};
};
Run Code Online (Sandbox Code Playgroud)