我正在尝试在这样的内部使用C++0x unique_ptr
类map
:
// compile with `g++ main.cpp -std=gnu++0x`
#include <string.h>
#include <map>
#include <memory>
using namespace std;
struct Foo {
char *str;
Foo(char const *str_): str(strdup(str_)) {}
};
int main(void) {
typedef std::map<int, unique_ptr<Foo>> Bar;
Bar bar;
auto a = bar.insert(Bar::value_type(1, new Foo("one")));
return 0;
}
但是 GCC 给了我以下错误(缩短,我认为这是相关部分,请在您自己的 C++ 编译器上测试):
main.cpp:19:从这里实例化 /usr/include/c++/4.4/bits/unique_ptr.h:214: 错误:删除函数'std::unique_ptr::unique_ptr(const std::unique_ptr&) [with _Tp = Foo, _Tp_Deleter = std::default_delete]' /usr/include/c++/4.4/bits/stl_pair.h:68:错误:在这里使用
我真的不确定我做错了什么,这适用于 MSVC。我发现了非常相似的问题,看起来很相似,但是他们的解决方案对我不起作用。
matt@stanley:/media/data/src/c++0x-test$ gcc --version gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3