为什么std :: map :: const_iterator在std :: for_each期间调用std :: pair构造函数,但是一个简单的for循环却没有?

Dan*_*aum 3 c++ lambda stl map c++11

我有一个稍微复杂的类数据成员,如下所示:

class BranchOutputRow
{
...
}

class Foo
{

public:

    // Slightly complex data member here
    std::map<boost::multiprecision::cpp_int, std::set<BranchOutputRow>> hits;

    void DoLoop1()
    {
        // This loop calls the std::pair<> constructor

        std::for_each(hits.cbegin(), hits.cend(),
        [&](std::pair<boost::multiprecision::cpp_int,
                      std::set<BranchOutputRow>> const & hit)
        {
            ...
        }
    }

    void DoLoop2()
    {
        // This loop does NOT call the std::pair<> constructor

        for (std::map<boost::multiprecision::cpp_int,
                      std::set<BranchOutputRow>>::const_iterator hitsPtr 
                    = hits.cbegin();
         hitsPtr != hits.cend();
         ++hitsPtr)
         {
             ...            
         }
    }

}

int main()
{
    Foo foo;
    foo.hits[1] = std::set<BranchOutputRow>();
    foo.hits[1].insert(BranchOutputRow());

    foo.DoLoop1(); // direct access to map object is not available
    foo.DoLoop2(); // direct access to map object is available
}
Run Code Online (Sandbox Code Playgroud)

如上所述,我发现Loop#1调用std::pair构造函数,尽管lambda函数通过引用接受其参数.因此,在循环1中,我没有直接访问地图中的对象,而只是一个副本.在我的实际计划中,我需要直接访问; 因此,我必须使用循环2指示的版本.

(事实上,回路2并没有调用std::pair构造函数-不是一个惊喜-并没有提供对地图对象的直接访问.)

我认为这std::for_each将被精心设计为提供与for循环2 相同的语义,因此不会调用std::pair构造函数,而是允许直接访问映射中的对象.

为什么Loop 1会调用std::pair构造函数,尽管lambda函数通过引用接受它的参数?

jro*_*rok 8

value_typestd::map<K,V>std::pair<const K, V>.你的lambda需要std::pair<K,V>.注意constness的差异.

转换是通过以下构造函数完成的:

template< class U1, class U2 >
pair( const pair<U1, U2>& p );
Run Code Online (Sandbox Code Playgroud)

(参见本参考页面上的(4))

转换的结果是临时的,临时文件可以绑定到const引用,因此您的代码可以正常工作.