I have a list of objects:
std::list<A> objects;
In order to trigger an async operation that will remove them from the list based on some condition, The objects need to be aware of their location in the list:
A::A(std::list<A>::iterator it);
How can I pass iterator to constructor when adding items to the list?
objects.emplace(<the iterator to new item>);
Initializing the member iterator of class A
cannot be done later, even the constructor may trigger the deletion operation, so it needs this member while construction.
Possible solutions that I have thought (brings extra complexity):
1:
objects.emplcae(A(objects.end())); // dummy object
A a{ std::prev(objects.end()) };
std::swap(a, object.back());
2:
objects.emplace();
objects.back().set_iterator(std::prev(objects.end()));
objects.back().run(); // Moved the work from constructor to this function (against RAII)
3: Try to get iterator based on the address of this
.