Having this class:
template<class T>
class Vec
{
public:
typedef T* iterator;
typedef T* const const_iterator;
typedef T value_type;
Vec() {
create();
}
explicit Vec(size_t n, const T& val = T()) {
create(n, val);
}
void clear() //here, I cannnot destroy nither by alloc.destroy, nor i->~T()
{
if (data)
{
iterator i = avail;
while (i != data)
{
alloc.destroy(--i); //or i->~T()
}
}
avail = data;
}
...
private:
iterator data;
iterator avail;
iterator limit;
std::allocator<T> alloc;
void create();
void create(size_t n, const T &val);
...
Now I will use the clear
functino (version of std::allocator<T>::destroy
, not destructor) in main file:
#include "vec2.hpp"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Vec<string> v(3, "abc");
v.clear();
cout << v[2] << endl;
}
1.)
Now even I have cleared the Vec
class (performing destructor for all elements in it), I can still output v[2] -> "abc"
even thought the string should perform the destructor and thus be empty string.
2.) should I use alloc.destroy(i)
, where the i
is T*
, or should I use i->~T()
?