Template of class pointers

This is a discussion on Template of class pointers within the C++ Programming forums, part of the General Programming Boards category; I want to create a template class that holds pointers to objects instead of objects themselves. I have this: Code: ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Template of class pointers

    I want to create a template class that holds pointers to objects instead of objects themselves.
    I have this:
    Code:
    template <class T>
    class MyADT { ... };
    MyADT<datatype> adt;
    But want this:
    Code:
    template <class T*> // incorrect syntax!!
    class MyADT { ... };
    MyADT<datatype*> adt;
    What is the correct syntax?
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You can store a T* as a member variable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    template <class T> // incorrect syntax!!
    class MyADT { ... };
    MyADT<datatype*> adt;
    Would be correct.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Although Raigne's example will allow your code to compile, I would point out that it is contrary to common practice for smart pointer class templates, e.g., we use std::auto_ptr<T> instead of std::auto_ptr<T*>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I'd like to ensure the class is never used with anything but pointers (i.e. MyADT<datatype> should be an error). I was under the impression this required a different syntax, as it was from an earlier discussion I ear-marked this from stroustrup's notes as a solution, but it's a been a while and I've forgotten how to harness it:
    Code:
    template<class T> inline void destroy(T*& p) { delete p; p = 0; }
    I noted to myself the T*& syntax was the key to solving the problem... but maybe I'm way off... does it in any way pertain to what I'm trying to do?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess you can use partial specialization:
    Code:
    #include <boost/static_assert.hpp>
    
    template <class T>
    class X
    {
        BOOST_STATIC_ASSERT(sizeof(T)==0 && "non-pointer argument to X");
    };
    
    template <class T>
    class X<T*>
    {
    };
    
    int main()
    {
        X<int*> y;  //OK
        //X<int> x;  //triggers STATIC_ASSERT
    }
    Another thing, if the class is meant for pointers, it might contain code that wouldn't compile anyway if the argument was not a pointer.

    Also, in the case of the destroy function, the template argument is a non-pointer type:
    Code:
    int* p = new int;
    destroy<int>(p);
    If you try to pass a non-pointer you'll get an error (no matching function to call).
    Last edited by anon; 11-07-2008 at 12:55 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Thanks. Just out of curiosity what is
    Code:
    template< class T* > class Foo{};
    saying anyways? It compiles fine, but instantiating an object of type Foo doesn't seem to be possible.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Nothing. It is a syntax error.
    Since a template declaration should specify placeholders for types, you cannot give them any explicit types such as pointer-types.
    So...
    Code:
    template<typename T> class Foo{};
    ...is fine, but...
    Code:
    template<typename T*> class Foo{};
    ...is not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Nothing. It is a syntax error.
    Maybe even a misunderstanding of the concept.
    Will this line _ever_ be valid, even if it were not a template parameter? No.

    This, however, is correct:
    Code:
    template<someClass* T>
    or this
    Code:
    template<someClass& T>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21