为什么 const std::pair<K,V>& 在 std::map 上基于范围的循环中不起作用?

SPA*_*SPA 5 c++ stdmap c++11 c++14

当访问的元素std::map通过const auto& entry在一个范围基于for循环我到在地图中的实际数据的参考。使用const std::pair<K,V>&,另一方面也没有给于数据的引用std::map

考虑这个例子(用 gcc 7.4 编译,-std=c++14)

#include <map>
#include <string>
#include <iostream>

int main(void)
{
    std::map<std::string, int> my_map {{"foo", 42}};
    for(const auto& entry : my_map)
        std::cout << entry.first << ' ' << entry.second << ' ' << &(entry.second) << std::endl;
    for(const std::pair<std::string, int>& entry : my_map)
        std::cout << entry.first << ' ' << entry.second << ' ' << &(entry.second) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

foo 42 0x11a7eb0
foo 42 0x7ffec118cfc0
Run Code Online (Sandbox Code Playgroud)

我知道std::mapvalue_type 是std::pair<const Key, T>. 但是我真的不明白在第二个基于范围的循环中发生了什么。

Kev*_*vin 5

std::map<K, V>::value_typestd::pair<const K, V>,不是std::pair<K, V>(参见cppreference

#include <map>
#include <string>
#include <iostream>

int main(void)
{
    std::map<std::string, int> my_map {{"foo", 42}};
    for(const auto& entry : my_map)
        std::cout << entry.first << ' ' << entry.second << ' ' << &(entry.second) << std::endl;
    for(const std::pair<std::string, int>& entry : my_map)
        std::cout << entry.first << ' ' << entry.second << ' ' << &(entry.second) << std::endl;
    for(const std::pair<const std::string, int>& entry : my_map)
        std::cout << entry.first << ' ' << entry.second << ' ' << &(entry.second) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

示例输出

foo 42 0x2065eb0
foo 42 0x7ffc2d536070
foo 42 0x2065eb0
Run Code Online (Sandbox Code Playgroud)

您的第二个循环有效,因为它正在创建一个临时std::pair<std::string, int>并将其绑定到您的参考(解释)。如果您尝试使用非常量引用,您会看到它失败(因为它无法绑定到临时引用):

错误:的类型的“参考无效初始化std::pair<std::__cxx11::basic_string<char>, int>&”从类型的表达式“ std::pair<const std::__cxx11::basic_string<char>, int>