unordered_map 的结构化绑定中的推导类型

Deduced type in structured binding of unordered_map

我正在尝试使用 autoauto &auto &&.

查看 unordered_map 的结构化绑定中的推导类型
#include <string>
#include <iostream>
#include <unordered_map>
#include <type_traits>

int main() {   

    std::unordered_map<std::string, std::string> m{{"a","a1"}, {"b","b1"}};

    for(auto &&  [k,v]:m)
    {
        std::cout << std::is_same<decltype(k), std::string const  >::value << '\n';
        std::cout << std::is_same<decltype(v), std::string >::value << '\n';

    }
}

无论我使用for(auto [k,v]:m)for(auto & [k,v]:m)for(auto && [k,v]:m),输出总是

1
1

我的问题是:

问题 1) 点赞指定here

1) If the argument is an unparenthesized id-expression naming a structured binding, then decltype yields the referenced type (described in the specification of the structured binding declaration).

here

Case 2: binding a tuple-like type [...] The referenced type for the i-th identifier is std::tuple_element<i, E>::type.

A std::pair(参见问题 2 的答案)实际上是 2 的元组。因此它是 'tuple-like'。

因此在这种情况下,Key 和 T 的基类型总是被返回(产生)。

问题2) 在内部 unordered_map 被分配为 std::pair<const Key, T>。因此,k 为 const.