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

Discussion in 'C++' started by nick4ng@googlemail.com, Nov 1, 2007.

  1. 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?

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

    Thanks
    n4ng
     
    , Nov 1, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    Re: knowing the instance of a template when compiling the destructorof a class

    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.
     
    Ian Collins, Nov 1, 2007
    #2
    1. Advertising

  3. Re: knowing the instance of a template when compiling the destructorof a class

    On 2007-11-01 10:50, 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<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.

    --
    Erik Wikström
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Nov 1, 2007
    #3
  4. Guest

    On 1 Nov, 10:14, Erik Wikström <> wrote:
    > On 2007-11-01 10:50, 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<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.
    >
    > --
    > Erik Wikström


    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
     
    , Nov 1, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Replies:
    16
    Views:
    784
    Pete Becker
    May 4, 2005
  2. frs
    Replies:
    20
    Views:
    792
    Alf P. Steinbach
    Sep 21, 2005
  3. iTooo
    Replies:
    4
    Views:
    536
    Peter_Julian
    Nov 5, 2005
  4. arun
    Replies:
    2
    Views:
    575
    benben
    Jun 13, 2006
  5. Skybuck Flying
    Replies:
    10
    Views:
    5,375
    Alf P. Steinbach /Usenet
    Jul 5, 2011
Loading...

Share This Page