GCC 4.4/4.5 unique_ptr对unordered_set/unordered_map不起作用

Kan*_* Li 6 c++ gcc g++ c++11

我有什么地方可以证实吗?我不确定这是GCC还是我的代码的问题.例如,以下代码无法编译:

#include <unordered_set>
#include <memory>
using namespace std;

int main() {
    unordered_set<unique_ptr<int> > s;
    unique_ptr<int> p(new int(0));
    s.insert(move(p));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误信息太大,我不想放在这里.GCC版本是4.5.3,编译标志是-std = gnu ++ 0x.也在4.4.5上测试过.

Luc*_*ton 8

GCC 4.6.1按原样接受你的代码,我发现它没有任何问题(即关联容器的value_type必须是EmplaceInsertable,std::unique_ptr并不能阻止它).据推测,这是GCC 4.5的缺陷.


cvo*_*scu 4

你的代码是正确的。这是 GCC 4.5 中的一个已知问题。它已在 4.6 中修复。请参阅http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44436。它也会影响有序容器(std::map、std::set 等)。可能最简单的解决方法(性能略有下降)是使用 std::shared_ptr 而不是 std::unique_ptr。