Thread: Template of class pointers

  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
    28,413
    You can store a T* as a member variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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*>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 01: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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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, 06:13 AM

Tags for this Thread