Do i need to call destructor when removing a class from vector?

I have a class PythonFunction that contains private vector filled with class PythonArgument values

there is a destructor in PythonArgument that does clean up my pointers, however I'll often need to change the values of that vector (the one from PythonFunction containing PythonArgument values) using function
int removeAllArguments();

is it enough to do something like this
1
2
3
4
5
6
7
8
9
int PythonFunction::removeAllArguments()
{
	unsigned size = args.size();

	//here will be several PythonArgument class values, these need to be cleaned up properly using destructor (or clean function)
	args.clear();

	return int(size - args.size());
}



or do I need to go thru every value in vector "args" and call a destructor for every value (for every PythonArgument) it contains? something like this:
1
2
3
4
5
6
7
8
9
10
11
int PythonFunction::removeAllArguments()
{
	unsigned size = args.size();

	for(unsigned i = size; i >= 0; i--)
		args[i].clean();

	args.clear();

	return int(size - args.size());
}


what I basically want to know is whether using vector::clear() does call the class destructor of the class that's within the vector, when it destroys the values in itself, or do I have to do that on my own before removing them from the vector?

edit: damn, 404 posts ! :D
Last edited on
http://www.cplusplus.com/reference/vector/vector/clear/

States:
may be optimized to constant complexity for trivially-destructible types (such as scalar or PODs), where elements need not be destroyed

In other words, clear() of non-trivially-destructible types, such as PythonFunction, cannot be optimized, because the objects are destroyed.

Anyway, this is easy to test:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>
#include <iostream>

struct Foo {
  ~Foo() { std::cout << "Dead\n"; }
};

int main () {
  std::vector<Foo> bar( 2 );
  bar.clear();
  std::cout << "End\n";
  return 0;
}

Dead
Dead
End
Last edited on
Topic archived. No new replies allowed.