Herb Sutter 在 Exceptional C++ 中写道auto_ptr
:
问题是 auto_ptr 不能完全满足可以放入容器的类型的要求,因为 auto_ptr 的副本不等价。
这本书是针对 C++03 编写的,我想知道这是否仍然有效,因为这段代码似乎可以在 GCC 4.7.1 下完美编译:
#include <vector>
struct Foo
{
Foo() { }
Foo( Foo&& ) { }
Foo( Foo& ) = delete;
Foo& operator= (Foo&&) { return *this; }
Foo& operator= (Foo&) = delete;
};
int main()
{
std::vector<Foo> bar;
bar.push_back(Foo());
}
但是接受可移动但不可复制的对象也可能是 GCC 扩展。我不确定。std::vector
对对象有什么要求?