2012-12-14 5 views
0

나는 값으로 std::shared_ptr<V>std::map을 저장할 것입니다.지도 <K, shared_ptr >을 (를) 매핑하려면 <K, shared_ptr <V>>을 전송 하시겠습니까?

하나의 함수에서이 맵에 대한 참조를 반환하지만 값 유형으로 std::shared_ptr<const V>을 사용할 수 있습니까? 여기

내가 달성하고자하는 것입니다 :

#include <map> 
#include <memory> 

struct A { 
    std::map<int, std::shared_ptr<int>> map; 

    const std::map<int, std::shared_ptr<const int>>& ret1() const { 
     return map; 
    } 
}; 

을하지만이 코드는 컴파일에 실패 :

error: invalid initialization of reference of type 
'const std::map<int, std::shared_ptr<const int> >&' 
from expression of type 'const std::map<int, std::shared_ptr<int> >' 

감사

답변

1

유형 Tconst T가 관련된 유형하지만 궁극적으로 다른 유형. ret1의 호출자가지도의 값을 수정하지 않도록하려면지도의 복사본을 반환하거나지도에 대한 참조를 인수로 받아 들여 키와 값을 복사합니다.

관련 문제