Reputation: 1583
Given std::vector<A>::iterator
and std::map<A, B>::iterator
, how do I explicitly call their destructors? I do not know the actual names of these types, because the ::iterator
member types are typedefs/aliases to implementation-specific classes.
I ask this question, because I am storing these iterators in an unrestricted c++ union and Visual Studio asks me to manually handle destruction. I could simply not call the destructor of the active element and assume iterators do not need clean-up, but that reeks of bad practice.
Upvotes: 0
Views: 1245
Reputation: 238391
Like this:
// let iter be vector<int>::iterator to be destroyed
iter.std::vector<int>::iterator::~iterator();
I do not know the actual names of these types, because the ::iterator member types are typedefs/aliases to implementation-specific classes.
Doesn't matter. Type aliases are type names as well.
LLVM appears to have a bug with this: https://bugs.llvm.org//show_bug.cgi?id=12350
Until it is fixed, as a workaround, introduce a non-nested alias:
using iterator = std::vector<int>::iterator;
iterator it;
it.iterator::~iterator();
or refer to the type as template argument (this code is from the bug report):
template <typename T> inline void placement_delete(T *x) { // C++ lacks placement delete, so fake it with a function x->~T(); }
Upvotes: 4
Reputation: 120001
std::destroy_at(&iter);
std::destroy
and friends are a C+17 feature. If your compiler lacks support it is trivial to implement yourself:
template <typename T>
void destroy_at(T* p) {
p->~T();
}
Upvotes: 3
Reputation: 91
Most often iterator are nothing but pointer to already allocated memory. Unless the iterators are created using new
typedef std::vector<A>::iterator VecIter;
VecIter* iter= new VecIter;
there is no need to delete. If you are using new, then you can call delete explicitly for the iterator variable.
delete iter;
Upvotes: 0