C++ 如何返回地图<;标准::字符串,唯一\u ptr>;元素,而不是失去它?

C++ 如何返回地图<;标准::字符串,唯一\u ptr>;元素,而不是失去它?,c++,templates,C++,Templates,我有一个模板类ConfigItem: // CConfigItems.hpp template< typename T > class ConfigItem : public IConfigItem // IConfigItem is its inteface { private: T m_value; public: ConfigItem< T >(const T& valueIn = 0) : m_value(valueIn) {}

我有一个模板类
ConfigItem

// CConfigItems.hpp
template< typename T > class ConfigItem : public IConfigItem // IConfigItem is its inteface
{
private:
    T m_value;

public:
    ConfigItem< T >(const T& valueIn = 0) : m_value(valueIn) {}

    operator T() const
    {
        return T(m_value);
    }
};

typedef std::map< std::string, std::unique_ptr< IConfigItem > > ConfigMap;

template< typename T, typename ...Args > std::unique_ptr< T > make_unique(Args&& ...args)
{
    return std::unique_ptr< T >(new T(std::forward< Args >(args)...));
}
当然,我在
ConfigMap
中有更多的元素和更多不同类型的元素。我有一个复制构造函数,但使用
unique\ptr
使我将其更改为move,这就是分段错误的问题。有没有关于如何解决这个问题的建议


这里发生了分段:

// ...
ConfigFile configFile(argv[2]);
std::shared_ptr< ISolution > solution;
initializeSolution(std::move(configFile), solution, displayer); // here the move destroys the m_configMap member. Then I am calling the getter
std::string clsfile = configFile.getClassifierCarFilePath();
// ...
/。。。
ConfigFile ConfigFile(argv[2]);
std::共享_ptr解决方案;
初始化解决方案(std::move(configFile)、解决方案、显示器);//在这里,移动将销毁m_configMap成员。那我就打电话给接线员
std::string clsfile=configFile.getClassifierCarFilePath();
// ...

因此,我在一个函数中移动ConfigFile对象,但在该函数之后需要它。是否有可能将
唯一\u ptr
更改为
共享\u ptr

为什么
返回T(m_值)
如果
m_value
已经是
T
类型?请详细说明您在哪里松开了它,我没有看到任何返回您在标题中提到的映射元素的代码。顺便问一下:
getClassifierCarFilePath
为什么不返回(
const
)引用?@AndyT:1。对不起,没必要。2. <代码>CheckEmptynes(*m_configMap[“path.to.value”])是否不删除指针?调用getter时出现分段错误。@重复数据消除程序:为什么要返回常量引用?仅当您返回输入参数时,有关返回的信息才应作为参考。
// ...
ConfigFile configFile(argv[2]);
std::shared_ptr< ISolution > solution;
initializeSolution(std::move(configFile), solution, displayer); // here the move destroys the m_configMap member. Then I am calling the getter
std::string clsfile = configFile.getClassifierCarFilePath();
// ...