0
我正在存儲一個std::map
的std::shared_ptr<V>
作爲值。Cast map <K,shared_ptr <V>>來映射<K,shared_ptr <const 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> >'
感謝