假设我想将 S
类型的对象静态转换为类型 T
并 move 分配结果:
template <typename S, typename T>
void f(T& t, S s);
我能想到四种写法:
template <typename S, typename T>
void f(T& t, S s) {
t = static_cast<T>(s);
t = static_cast<T>(std::move(s));
t = static_cast<std::remove_reference_t<T>&&>(s);
t = static_cast<std::remove_reference_t<T>&&>(std::move(s));
}
这四行中的一部分或全部做同样的事情吗?首选的方法是什么?
最佳答案
Do some or all of these four lines do the same thing?
它们全部 move 赋值 t
。第三和第四处的转换是多余的。第一个将参数复制到临时变量中,第二个 move 。二是优越。虽然另外一个问题是,是否首先需要该功能。
关于c++ - static_cast 为不同的类型,并 move 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54191660/