How can I have a pair with reference inside vector?

前端 未结 1 829
别跟我提以往
别跟我提以往 2020-12-22 08:06

I really need to make std::vector of std::pair with reference (&) inside, but it breaks inside the function when I try to pu

相关标签:
1条回答
  • 2020-12-22 08:35

    Since C++14 std::make_pair is defined as

    template< class T1, class T2 >
    std::pair<V1,V2> make_pair( T1&& t, T2&& u );
    

    where V1 and V2 are std::decay<T1>::type and std::decay<T2>::type respectively.

    This means that your make_pair<const int&, int> calls do not really produce pairs with references as their first elements (contrary to what you apparently believed). They actually produce temporaries of pair<int, int> type. At this point you lose any attachment to the original int object stored in your unique_ptr.

    When you pass these pair<int, int> temporaries to push_back, they get implicitly converted to temporaries of pair<const int&, int> type, which is your vector's element type. Through this mechanism you attach references inside your vector's element to int members of those pair<int, int> temporaries produced by make_pair (not to int objects stored in your unique_ptrs). Once the temporaries expire, the references go sour.


    In this case you can eliminate this specific problem by avoiding make_pair altogether and simply directly constructing std::pair objects of proper type, e.g.

    vector.push_back(std::pair<const int&, int>(*ptr, 11));
    

    but you might run into other problems caused by raw references later.

    0 讨论(0)
提交回复
热议问题