我想使用一个std::unordered_map<unsigned,std::ofstream>
,但是失败了。现在我想知道这是不是根本不可能,或者是编译器问题,还是我只是没有弄对。问题在于插入另一个元素:
std::ofstream&get(unsigned key, std::unordered_map<unsigned,std::ofstream>&map)
{
auto file = map.find(key);
if(file==map.end()) {
map.emplace(std::make_pair(key,std::ofstream{})); // <-- trouble here
file = map.find(key);
assert(file!=map.end());
}
return file->second;
}
使用gcc(4.8.1)时会失败,因为std::ofstream
不可复制。然而,它是可移动的,所以应该有一个解决方案,或者没有?(我想要插入的只是一个默认构造的std::ofstream
)。我尝试添加一些std::move()
,但那没有帮助。
我刚刚注意到上面的代码在clang 3.4中确实编译了。这不对吗?