【发布时间】:2014-03-15 19:49:22
【问题描述】:
我正在尝试这样的事情:
class MyClass
{
public:
explicit MyClass(int) {...};
MyClass(MyClass&& that) { swap(that); }
private:
MyClass(const MyClass&); // disabled, pre-C++11 syntax
MyClass& operator=(const MyClass&); // disabled, pre-C++11 syntax
};
现在我有一个列表,我通过 emplace 将它们插入其中,我正在尝试做这样的事情。
std::list<MyClass> lst;
std::remove_if(lst.begin(), lst.end(), [&,this](MyClass& mcl) { return mcl.is_foo();});
在 gcc 4.6.x 上,我不断收到此错误:
In file included from /usr/include/c++/4.6/algorithm:63:0,
from simple_file_cache.cpp:5:
file_cache_entry.h: In function ‘_FIter std::remove_if(_FIter, _FIter, _Predicate)
[with_FIter = std::_List_iterator<MyClass>, _Predicate =
AnotherClass::foo_bar(std::tuple<unsigned int, unsigned int>)::<lambda(MyClass&)>]’:
anotherclass.cpp:225:11: instantiated from here
anotherclass.h:68:18: error: ‘MyClass& MyClass::operator=(const MyClass&)’ is private
/usr/include/c++/4.6/bits/stl_algo.h:1149:13: error: within this context
make: *** [simple_file_cache.o] Error 1
为什么要找拷贝构造函数?
【问题讨论】:
-
std::remove或std::remove_if?您的标题和代码不匹配。 -
另请注意,GCC 4.6 中的 C++11 支持受到严格限制。
-
实际上,
std::remove_if上的std::list对我来说没有意义。我宁愿使用std::list::erase或std::list::remove_if。 -
“它为什么要寻找拷贝构造函数?” 不是。它正在寻找一个赋值函数。
-
来自 C++ 标准(草案),[alg.remove]/6 关于
remove_if: "注意: 范围[ret,last)中的每个元素,其中@987654332 @ 是返回值,具有有效但未指定的状态,因为算法可以通过从最初在该范围内的元素移动来消除元素。”