私は構造体キャッシュのベクトルを持っています。 Moveコンストラクタを実装するキャッシュ。このベクトルを反復して特定の条件で要素を消去しようとすると、コンパイルに失敗します。イテレータを使用してベクトル要素を消去することはできません。ベクトル要素は移動コンストラクタを実装します
struct Cache {
Cache() {}
Cache(Cache&& other)
: serviceName(std::move(other.serviceName))
{}
~Cache() {}
size_t referenceCount;
std::string serviceName;
};
std::vector<Cache> vectorCaches;
auto iter = vectorCaches.begin();
while(iter != vectorCaches.end()) {
if(iter->serviceName == "Sample") {
iter = vectorCaches.erase(iter); //compiler error here
} else {
iter++;
}
}
コンパイラエラー:
error: use of deleted function 'Cache& Cache::operator=(const Cache&)' __result = std::move(__first); ^note: 'Cache& Cache::operator=(const Cache&)' is implicitly declared as deleted because 'Cache' declares a move constructor or move assignment operator struct Cache {
ベクトル要素は、コンストラクタを動かす実装したときに消去を達成するための任意の回避策はありますか?どんな助けもありがとうございます。
移動コンストラクタ「noexcept」を作成してみてください。 –
@KerrekSB OPが移動割当演算子を定義する必要はありませんか? – NathanOliver
@ NathanOliver:確かにそうです。 –