【发布时间】:2010-06-18 16:35:42
【问题描述】:
我有一些继承 boost::noncopyable 的预定义类型(所以我必须将指针存储在这些对象上)。我使用 boost::ptr_map。据我所知,其中的第二个参数已经是一个指针。所以,代码:
ptr_map<string, boost::any> SomeMap;
typedef %Some noncopyable class/signature% NewType;
// Inserting now
boost::any *temp = new boost::any(new KeyEvent());
SomeMap.insert("SomeKey", temp);
错误是:
error: no matching function for call to ‘boost::ptr_map<std::basic_string<char>, boost::any>::insert(const char [11], boost::any*&)’
UPD:当我没有将指针传递给任何any temp = any(new KeyEvent());时
我明白了:
error: no matching function for call to ‘boost::ptr_map<std::basic_string<char>, boost::any>::insert(const char [11], boost::any&)’
【问题讨论】:
-
为什么不
SomeMap["SomeKey"] = temp;? -
@Cubbi:这将编译,但它不会是异常安全的。如果字符串构造函数或空元素插入抛出,
temp将泄漏。 -
@Mike Seymour:你是对的。这就是我多年来只使用智能指针的结果。
标签: c++ boost types insert boost-ptr-container