我试图通过使用 和 来查看 unordered_map 的结构化绑定中的auto推导auto &类型auto &&。
#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)or for(auto & [k,v]:m)or for(auto && [k,v]:m),输出总是
1
1
我的问题是:
为什么在or的情况下
decltype(k)anddecltype(v)不是引用类型?for(auto & [k,v]:m)for(auto && [k,v]:m)为什么
decltype(k)是类型const的情况下for(auto [k,v]:m)?