[英]Static_cast converts to wrong data type, but the result is still correct?
[英]static_cast to a different type, and move the result
假設我想static_cast一個S
類型的對象來鍵入T
並移動分配結果:
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));
}
這四條線中的部分或全部是否做同樣的事情? 最好的方法是什么?
這四條線中的部分或全部是否做同樣的事情?
所有這些都移動 - 分配t
。 第三和第四次轉換是多余的。 第一個將參數復制到臨時,第二個移動。 第二是優越。 雖然另一個問題是,首先是否需要該功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.