Typedef of a template?

Discussion in 'C++' started by Richard van Wegen, Jul 14, 2003.

  1. Dear All

    I'm hoping someone can show me the correct way to typedef a template
    class -
    i.e. something along the lines of

    typedef boost::shared_ptr<T> shared_ptr_multithread<T>;

    (syntax is obviously wrong but you get the idea)

    i.e. I want to be able to write

    int main()
    {
    shared_ptr_multithread<MyClass> my_class_multithreaded_ptr;
    shared_ptr<MyClass> my_class_singlethreaded_ptr;
    }

    so that for the time being it is equivalent to
    int main()
    {
    shared_ptr<MyClass> my_class_multithreaded_ptr;
    shared_ptr<MyClass> my_class_singlethreaded_ptr;
    }

    but later on I can change the way that shared_ptr_multithread<MyClass>
    is
    implemented without affecting all my other shared_ptr<MyClass>
    declarations.

    I'm sure it must be simple, but I can't find the syntax for it
    anywhere and I've tried all the combinations of "template" and
    "typename" etc I can think of with no success. (GCC 2.96). At the
    moment I'm forced to rely on #define (eugh).

    Thanks for any pointers!
    Richard
     
    Richard van Wegen, Jul 14, 2003
    #1
    1. Advertising

  2. "Richard van Wegen" <> wrote in message
    news:...
    > Dear All
    >
    > I'm hoping someone can show me the correct way to typedef a template
    > class -
    > i.e. something along the lines of
    >
    > typedef boost::shared_ptr<T> shared_ptr_multithread<T>;
    >
    > (syntax is obviously wrong but you get the idea)
    >
    > i.e. I want to be able to write
    >
    > int main()
    > {
    > shared_ptr_multithread<MyClass> my_class_multithreaded_ptr;
    > shared_ptr<MyClass> my_class_singlethreaded_ptr;
    > }
    >
    > so that for the time being it is equivalent to
    > int main()
    > {
    > shared_ptr<MyClass> my_class_multithreaded_ptr;
    > shared_ptr<MyClass> my_class_singlethreaded_ptr;
    > }
    >
    > but later on I can change the way that shared_ptr_multithread<MyClass>
    > is
    > implemented without affecting all my other shared_ptr<MyClass>
    > declarations.
    >
    > I'm sure it must be simple, but I can't find the syntax for it
    > anywhere and I've tried all the combinations of "template" and
    > "typename" etc I can think of with no success. (GCC 2.96). At the
    > moment I'm forced to rely on #define (eugh).
    >
    > Thanks for any pointers!
    > Richard


    You cannot write template typedefs, its a limitiation of the C++ syntax
    (soon to be corrected?).

    The best you can do is wrap your typedefs in a struct, which can be
    templated.

    template <class T>
    struct Type
    {
    typedef boost::shared_ptr<T> shared_ptr_multithread;
    typedef boost::shared_ptr<T> shared_ptr; // or something
    };

    int main()
    {
    Type::shared_ptr_multithread<MyClass> my_class_multithreaded_ptr;
    Type::shared_ptr<MyClass> my_class_singlethreaded_ptr;
    }

    john
     
    John Harrison, Jul 14, 2003
    #2
    1. Advertising

  3. "Sarah Thompson" <> wrote in message
    news:betma4$n2m$1$...
    >
    >
    > John Harrison wrote:
    > > You cannot write template typedefs, its a limitiation of the C++ syntax
    > > (soon to be corrected?).
    > >
    > > The best you can do is wrap your typedefs in a struct, which can be
    > > templated.
    > >
    > > template <class T>
    > > struct Type
    > > {
    > > typedef boost::shared_ptr<T> shared_ptr_multithread;
    > > typedef boost::shared_ptr<T> shared_ptr; // or something
    > > };
    > >
    > > int main()
    > > {
    > > Type::shared_ptr_multithread<MyClass> my_class_multithreaded_ptr;
    > > Type::shared_ptr<MyClass> my_class_singlethreaded_ptr;
    > > }

    >
    > That should be written as:
    >
    > int main()
    > {
    > Type<MyClass>::shared_ptr_multithread my_class_multithreaded_ptr;
    > Type<MyClass>::shared_ptr my_class_singlethreaded_ptr;
    > }
    >
    > shouldn't it?
    >


    Yes indeedy, thanks.

    john
     
    John Harrison, Jul 14, 2003
    #3
  4. > > > You cannot write template typedefs, its a limitiation of the C++ syntax
    > > > (soon to be corrected?).
    > > >
    > > > The best you can do is wrap your typedefs in a struct, which can be
    > > > templated.
    > > >
    > > > template <class T>
    > > > struct Type
    > > > {
    > > > typedef boost::shared_ptr<T> shared_ptr_multithread;
    > > > typedef boost::shared_ptr<T> shared_ptr; // or something
    > > > };

    > >
    > > int main()
    > > {
    > > Type<MyClass>::shared_ptr_multithread my_class_multithreaded_ptr;
    > > Type<MyClass>::shared_ptr my_class_singlethreaded_ptr;
    > > }


    Ah ok, that explains why I was having so much trouble.

    Thanks very much to you both for the help!

    Cheers
    Richard
     
    Richard van Wegen, Jul 15, 2003
    #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. Robert A. T. Kaldy

    template typedef as return type

    Robert A. T. Kaldy, Jul 9, 2003, in forum: C++
    Replies:
    1
    Views:
    417
    Michael Kochetkov
    Jul 9, 2003
  2. Arkadiy Vertleyb
    Replies:
    5
    Views:
    2,329
    Arkadiy Vertleyb
    Sep 27, 2003
  3. Adam Nielsen
    Replies:
    3
    Views:
    411
  4. oor
    Replies:
    0
    Views:
    1,356
  5. Replies:
    1
    Views:
    608
Loading...

Share This Page