473,511 Members | 17,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Nov 1 '07 #1
3 1701
ni*****@googlemail.com wrote:
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?

--
Ian Collins.
Nov 1 '07 #2
On 2007-11-01 10:50, ni*****@googlemail.com wrote:
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<doubled;
Foo<inti;
}

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.

--
Erik Wikström
Nov 1 '07 #3
On 1 Nov, 10:14, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-11-01 10:50, nick...@googlemail.com wrote:
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<doubled;
Foo<inti;

}

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.

--
Erik Wikström
The problem is that I can not use templated class I can have only
templated constructor

class MyClass
{
template <typename TMyClass(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

Nov 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8085
by: Bruce Lee Roy | last post by:
Hi, I am using visual studio.net and I am having problems using this bit of code when I create an instance, //////////////////////////////////////////// template<class T> class matrix : public...
4
2333
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a...
6
1687
by: Gonçalo Rodrigues | last post by:
Hi all, The following (test) program is not working as I expected. Note: The code is a bit long but is easy to understand: There is a class Object implementing ref counting. A Ref<T> template...
4
1674
by: Rui Maciel | last post by:
I'm trying to write a tree class and I've just stumbled on a compiling problem. The compiler throws an error on a piece of code which to my eyes it is error-free. Can anyone help me with this? ...
3
3739
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
13
5364
by: JohnQ | last post by:
Why would anyone write: class SomeThing // class littered with non-domain single-instancing code :( { private: SomeThing(); static SomeThing* pInstance_; public: static SomeThing*...
2
3520
by: parvtb | last post by:
I have the following code: #include <list> using namespace std; template <typename T> list<T>::iterator doStuff( list<T>::iterator myIter ) //--error here
7
7321
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
7
5756
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.