Function for deleting pointers of the same general type

So I wrote this function to delete, N, amount of pointers of the same type.

I was just wondering if this is valid?

I checked for memory leaks using visual studio's memory leak debugging tools and libraries, and I didn't find any. All feedback is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
template<class T>
void deletePtr(size_t count,...)
{
	try {
		va_list arguments;
		va_start(arguments, count);
		for (size_t i = 0; i < count; i++) {
			delete va_arg(arguments, T);
		}
		va_end(arguments);
	}
	catch (...) {}
}
Why would you want to do this?
@helios thanks for the response btw, and Merry Christmas!!!
I don't have any particular need to do this, apart from making code written a little more aesthetically pleasing, instead of having something like this:
1
2
3
4
5
6
7
delete a;
delete b;
delete c;
delete d;
delete e;
delete x;
delete y;

which was actually part of my code it can simply be replaced by:

1
2
deletePtr<ArrayList<void*>*>(3, a,x,y);
deletePtr<LinkedList<void*>*>(4,b,c,d,e);

Last edited on
Variadic functions are a historical artifact. No code that doesn't use them can be improved by starting to use them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <list>
#include <memory>

struct A { int v ; A( int i = 0 ) : v(i) {} ~A() { std::cout << "A::destructor - v == " << v << '\n' ; } };

int main()
{
    {
        std::list< std::unique_ptr<A> > lst ; // *** good: list of smart pointers
        for( int i = 0 ; i < 5 ; ++i ) lst.push_back( std::make_unique<A>(i) ) ;
    }
    std::cout << '\n' ;
    {
        std::list<A*> lst2 ; // *** bad: list of owning raw pointers
        for( int i = 5 ; i < 10 ; ++i ) lst2.push_back( new A(i) ) ;

        for( auto p : lst2 ) delete p ;
    }
}

http://coliru.stacked-crooked.com/a/64126ab85568b499
@helios tbh I was just digging around the internet to see what interesting features of c++ I could find that could "spice up" my code, but now I understand why so many people dislike variadic functions. They're outdated!

@JLBorges I actually am using smart pointers in any implementation I use the ArrayList/LinkedList classes with, but for the purposes of sticking something into the template arguments for the class I stuck void* because it wasn't the objects inside I was worried about deleting, but rather just the encasing class which I wanted to delete. The actual lists had different POD types in them but not necessarily the same data types. I appreciate the reply though.
No, no one likes because they're massively unsafe, and using them requires getting a number of things right that the compiler can't verify, and if you screw up you end up corrupting the stack.
Topic archived. No new replies allowed.