[問題] C++17 Structured binding 型別無法理解

看板C_and_CPP作者 (JOMI)時間5年前 (2019/06/23 03:00), 編輯推噓5(6127)
留言34則, 7人參與, 5年前最新討論串1/3 (看更多)
最近會看到一些c++17語法 想說來研究一下 https://en.cppreference.com/w/cpp/language/structured_binding 網路上介紹的文章許多 但都完全只是"介紹" 我實際上遇到一些怪異的型別推導結果 完全無法歸納規則 可能變成 知道可以用 但不敢亂用.... 也許cppref 有介紹的很完整但我實在是看不太懂他表達的 舉幾個例子 1. 這屬於網頁上的case幾?我不知道.... std::map<int, int> m; for (auto& [k, v] : m) { k = 123; } k 是const& 變成不能改 ??? why.... 好那我 std::map<int, int> m; for (auto [k, v] : m) { k = 123; } k是const int....哪來的const.... 2. int a = 1, b = 2; const auto& [x, y] = std::tie(a, b); x = 5566; 一臉就是const! 但竟然x是 int&.....可以改 why....+2 好那我 auto [z, w] = std::tie(a, b); z = 123; 我什麼都不加....乍看就是int z竟然是int&....我不小心改到了a..... 這我可能還可以理解 他會去decltype(z) 結果是int& 但實在不好讀也很容易誤用 還有很多看不是很懂... 總覺得找不到可以簡單記憶的規則 連VC滑鼠移過去顯示的型態也是錯的.... 請問版上有人能通透理解這些規則嗎@@ 不然我還是覺得寫17以前寫法 for (const auto& p : map) 我可以明確知道他在寫什麼好懂許多 討論一下~ 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.242.129.39 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1561230037.A.5C6.html

06/23 05:52, 5年前 , 1F
因為 k, v 的 type 要查 map 的 iterator 指向什麼呀
06/23 05:52, 1F

06/23 05:52, 5年前 , 2F
for-range 轉成 for + begin/end, 然後 map 的
06/23 05:52, 2F

06/23 05:54, 5年前 , 3F
key是const是map的性質
06/23 05:54, 3F

06/23 05:55, 5年前 , 4F
value_type 是 std::pair<const Key, T>, 所以自然由
06/23 05:55, 4F

06/23 05:56, 5年前 , 5F
auto& [k,v] = *it 知道 k 是 const
06/23 05:56, 5F

06/23 05:56, 5年前 , 6F
pair<const K, T>
06/23 05:56, 6F

06/23 06:04, 5年前 , 7F
tuple<Types&...> tie( Types&... args )
06/23 06:04, 7F

06/23 12:12, 5年前 , 8F
晚點理解一下 但意思是我無法馬上從code一眼看出型別 這
06/23 12:12, 8F

06/23 12:12, 5年前 , 9F
樣好嗎?
06/23 12:12, 9F

06/23 12:13, 5年前 , 10F
而且2. 我寫了const 竟然沒用,感覺很容易誤會
06/23 12:13, 10F

06/23 13:36, 5年前 , 11F
我覺得這有一半是tie的問題..
06/23 13:36, 11F

06/23 13:36, 5年前 , 12F
2的a是const ref to int, 不是ref to const int
06/23 13:36, 12F

06/23 13:37, 5年前 , 13F
x 打錯
06/23 13:37, 13F

06/23 16:16, 5年前 , 14F
先搞懂map是啥
06/23 16:16, 14F

06/23 16:24, 5年前 , 15F
我查你貼的連結, 提到 cv-auto 的地方在 array type A
06/23 16:24, 15F

06/23 16:28, 5年前 , 16F
那裡, 所以看來是用到 array 上的時候才看得出來?
06/23 16:28, 16F

06/23 16:29, 5年前 , 17F
我猜 const 不是加到 [???,???,...] 裡面的 identifier
06/23 16:29, 17F

06/23 16:29, 5年前 , 18F
所以才會乍看之下沒有效
06/23 16:29, 18F

06/23 16:32, 5年前 , 19F
key是const 但我如果是by value copy 他不該保留const
06/23 16:32, 19F

06/23 16:32, 5年前 , 20F
吧?
06/23 16:32, 20F

06/23 16:43, 5年前 , 21F
可是看他 case 2 寫的, 不管前面有沒有 cv 或不管有沒有
06/23 16:43, 21F

06/23 16:43, 5年前 , 22F
&, && 都是 "reference to std::tuple_element<i, E>::ty
06/23 16:43, 22F

06/23 16:44, 5年前 , 23F
@fenikso: const ref 沒這種東西吧?一般語法也寫不出in
06/23 16:44, 23F

06/23 16:44, 5年前 , 24F
t &const foo; 還是這裡有什麼高深原理?
06/23 16:44, 24F

06/23 18:25, 5年前 , 25F
map與pair都不是array type
06/23 18:25, 25F

06/23 18:25, 5年前 , 26F
所以不會copy行為
06/23 18:25, 26F

06/23 19:29, 5年前 , 27F
為何用std::tie,用std::tuple不好嗎
06/23 19:29, 27F

06/23 20:14, 5年前 , 28F
map系列的key都是const 實際上這也很合理
06/23 20:14, 28F

06/23 20:57, 5年前 , 29F
對, 一般語法寫不出const ref, 但是在裡面有type alias的
06/23 20:57, 29F

06/23 20:57, 5年前 , 30F
情況下會允許這種組合出現, 然後compiler會好心的幫你把
06/23 20:57, 30F

06/23 20:57, 5年前 , 31F
const丟掉
06/23 20:57, 31F

06/23 20:59, 5年前 , 32F
例如說你可以寫 using T = int&; using U = const T;
06/23 20:59, 32F

06/23 21:00, 5年前 , 33F
這時候U = const T = const ref to int = ref to int;
06/23 21:00, 33F

06/24 05:17, 5年前 , 34F
謝謝 原來這樣會去掉const
06/24 05:17, 34F
文章代碼(AID): #1T3dhLN6 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1T3dhLN6 (C_and_CPP)