knowing the instance of a template when compiling the destructor of a class

N

nick4ng

If I have a templated constructor, there is a way to know, at compile
time, the instance when compiling the destructor?

To be clearer my problem is that I have a templated constructor that
is empty for particular arguments. I need to have an empty destructor
in this case as well.

Is it possible?

I should add that for other reasons I can not have a templated class.

Thanks
n4ng
 
I

Ian Collins

If I have a templated constructor, there is a way to know, at compile
time, the instance when compiling the destructor?

To be clearer my problem is that I have a templated constructor that
is empty for particular arguments. I need to have an empty destructor
in this case as well.

Is it possible?
Your question doesn't make a great deal of sense, can you provide an
example?
 
G

Guest

If I have a templated constructor, there is a way to know, at compile
time, the instance when compiling the destructor?

To be clearer my problem is that I have a templated constructor that
is empty for particular arguments. I need to have an empty destructor
in this case as well.

Is it possible?

You mean something like this:

#include <iostream>

template<typename T>
class Foo
{
T* t;
public:
Foo();
~Foo();
};

template<typename T>
Foo<T>::Foo()
{
t = new T(); // Should use initialisation list
}

template<typename T>
Foo<T>::~Foo()
{
delete t;
}

template<>
Foo<int>::Foo()
{
}

template<>
Foo<int>::~Foo()
{
}

int main()
{
Foo<double> d;
Foo<int> i;
}

When instantiating Foo with an int the special constructor (and later
destructor) will be used. Notice however that I think it is a bad idea
to specialise the destructor if it can be avoided. It is better to just
set t to 0 in Foo<int>'s constructor and not specialise the destructor.
 
N

nick4ng

You mean something like this:

#include <iostream>

template<typename T>
class Foo
{
T* t;
public:
Foo();
~Foo();

};

template<typename T>
Foo<T>::Foo()
{
t = new T(); // Should use initialisation list

}

template<typename T>
Foo<T>::~Foo()
{
delete t;

}

template<>
Foo<int>::Foo()
{

}

template<>
Foo<int>::~Foo()
{

}

int main()
{
Foo<double> d;
Foo<int> i;

}

When instantiating Foo with an int the special constructor (and later
destructor) will be used. Notice however that I think it is a bad idea
to specialise the destructor if it can be avoided. It is better to just
set t to 0 in Foo<int>'s constructor and not specialise the destructor.

The problem is that I can not use templated class I can have only
templated constructor

class MyClass
{
template <typename T> MyClass(T){ /*Use T as argument for
templates*/}
~MyClass(){ /*Do default action or nothing for particular T used in
the ctor*/}
};

The reason why I can not use template class is that I need to store
them in a Loki::Typelist.
What I'm thinking now is to rely on the compiler optimisation: I set a
non static const in the ctor and I check it in the destructor. This
means that the check is not made at compile time if the compiler does
not optimize the code.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top