Re: [問題] 什麼時候用 auto&&?
這也是我在實務上無法想到用途的一個語法(也是知道它的意思 但還是不知道啥時該用)
https://en.cppreference.com/w/cpp/language/reference
2) auto&& except when deduced from a brace-enclosed initializer list:
這邊寫到
for (auto&& x: f()) {
// x is a forwarding reference; this is the safest way to use range for
loops
}
為什麼說這是最safest的? 如果只是readonly 的話寫const auto&不是更好?
另外是他寫
auto&& z = {1, 2, 3}; // *not* a forwarding reference (special case for
initializer lists)
為什麼說"不是"?
https://ideone.com/fYlKY4
這樣驗證感覺就一樣啊? special在哪?
但看完還是不知道什麼時候必須用auto&&
如果說
一個函數 Type Func();
我用auto&& temp = Func(); or auto temp = Func();
然後把它std::move(temp) 給其他函數
前者可能少一次move construct
但印象中有文章說 這樣反而會讓optimization 受限?
但其實實務上比較會寫 const auto& temp = Func();
然後在copy給其他函數
這樣寫起來反而變成一定要用copy了? 這樣有了move 是不是根本就不要加上const&
而是都用auto temp = Func(); 這樣來反而好?
問題都是從
https://en.cppreference.com/w/cpp/utility/any/any_cast
我不知道要寫auto temp = std::any_cast<....>(...); 還是加上&&
以上諸多盲點
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 110.28.13.96
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1551106734.A.A14.html
※ 編輯: lovejomi (110.28.13.96), 02/25/2019 23:11:54
→
02/26 00:21,
5年前
, 1F
02/26 00:21, 1F
→
02/26 09:32,
5年前
, 2F
02/26 09:32, 2F
→
02/26 09:53,
5年前
, 3F
02/26 09:53, 3F
→
02/26 09:53,
5年前
, 4F
02/26 09:53, 4F
→
02/26 09:53,
5年前
, 5F
02/26 09:53, 5F
→
02/26 09:53,
5年前
, 6F
02/26 09:53, 6F
→
02/26 11:18,
5年前
, 7F
02/26 11:18, 7F
→
02/26 11:18,
5年前
, 8F
02/26 11:18, 8F
→
02/26 12:14,
5年前
, 9F
02/26 12:14, 9F
→
02/26 12:16,
5年前
, 10F
02/26 12:16, 10F
→
02/26 12:16,
5年前
, 11F
02/26 12:16, 11F
→
02/26 18:58,
5年前
, 12F
02/26 18:58, 12F
→
02/26 18:59,
5年前
, 13F
02/26 18:59, 13F
→
02/26 21:35,
5年前
, 14F
02/26 21:35, 14F
→
02/26 21:36,
5年前
, 15F
02/26 21:36, 15F
→
02/26 21:38,
5年前
, 16F
02/26 21:38, 16F
→
02/26 21:40,
5年前
, 17F
02/26 21:40, 17F
→
02/26 21:41,
5年前
, 18F
02/26 21:41, 18F
→
02/26 21:42,
5年前
, 19F
02/26 21:42, 19F
→
02/26 21:42,
5年前
, 20F
02/26 21:42, 20F
→
02/26 21:44,
5年前
, 21F
02/26 21:44, 21F
→
02/26 21:44,
5年前
, 22F
02/26 21:44, 22F
→
02/26 21:56,
5年前
, 23F
02/26 21:56, 23F
→
02/26 21:56,
5年前
, 24F
02/26 21:56, 24F
→
02/26 23:24,
5年前
, 25F
02/26 23:24, 25F
→
02/26 23:34,
5年前
, 26F
02/26 23:34, 26F
→
02/26 23:35,
5年前
, 27F
02/26 23:35, 27F
→
02/27 00:06,
5年前
, 28F
02/27 00:06, 28F
→
02/27 00:09,
5年前
, 29F
02/27 00:09, 29F
→
02/27 00:11,
5年前
, 30F
02/27 00:11, 30F
→
02/27 00:12,
5年前
, 31F
02/27 00:12, 31F
→
02/27 00:13,
5年前
, 32F
02/27 00:13, 32F
→
02/27 00:14,
5年前
, 33F
02/27 00:14, 33F
→
02/27 00:14,
5年前
, 34F
02/27 00:14, 34F
→
02/27 00:32,
5年前
, 35F
02/27 00:32, 35F
→
02/27 00:33,
5年前
, 36F
02/27 00:33, 36F
推
02/27 00:38,
5年前
, 37F
02/27 00:38, 37F
→
02/27 12:17,
5年前
, 38F
02/27 12:17, 38F
→
02/27 19:00,
5年前
, 39F
02/27 19:00, 39F
→
02/27 19:00,
5年前
, 40F
02/27 19:00, 40F
→
02/27 19:05,
5年前
, 41F
02/27 19:05, 41F
→
02/27 19:05,
5年前
, 42F
02/27 19:05, 42F
→
02/27 19:05,
5年前
, 43F
02/27 19:05, 43F