Typedef of a template?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard van Wegen

    Typedef of a template?

    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_p tr<T> shared_ptr_mult ithread<T>;

    (syntax is obviously wrong but you get the idea)

    i.e. I want to be able to write

    int main()
    {
    shared_ptr_mult ithread<MyClass > my_class_multit hreaded_ptr;
    shared_ptr<MyCl ass> my_class_single threaded_ptr;
    }

    so that for the time being it is equivalent to
    int main()
    {
    shared_ptr<MyCl ass> my_class_multit hreaded_ptr;
    shared_ptr<MyCl ass> my_class_single threaded_ptr;
    }

    but later on I can change the way that shared_ptr_mult ithread<MyClass >
    is
    implemented without affecting all my other shared_ptr<MyCl ass>
    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
  • John Harrison

    #2
    Re: Typedef of a template?


    "Richard van Wegen" <spamblackhole@ volcanomail.com > wrote in message
    news:c74f7092.0 307132234.507c5 096@posting.goo gle.com...[color=blue]
    > 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_p tr<T> shared_ptr_mult ithread<T>;
    >
    > (syntax is obviously wrong but you get the idea)
    >
    > i.e. I want to be able to write
    >
    > int main()
    > {
    > shared_ptr_mult ithread<MyClass > my_class_multit hreaded_ptr;
    > shared_ptr<MyCl ass> my_class_single threaded_ptr;
    > }
    >
    > so that for the time being it is equivalent to
    > int main()
    > {
    > shared_ptr<MyCl ass> my_class_multit hreaded_ptr;
    > shared_ptr<MyCl ass> my_class_single threaded_ptr;
    > }
    >
    > but later on I can change the way that shared_ptr_mult ithread<MyClass >
    > is
    > implemented without affecting all my other shared_ptr<MyCl ass>
    > 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[/color]

    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_p tr<T> shared_ptr_mult ithread;
    typedef boost::shared_p tr<T> shared_ptr; // or something
    };

    int main()
    {
    Type::shared_pt r_multithread<M yClass> my_class_multit hreaded_ptr;
    Type::shared_pt r<MyClass> my_class_single threaded_ptr;
    }

    john


    Comment

    • John Harrison

      #3
      Re: Typedef of a template?


      "Sarah Thompson" <sarah@telergy. remove.this.bit .com> wrote in message
      news:betma4$n2m $1$830fa79f@new s.demon.co.uk.. .[color=blue]
      >
      >
      > John Harrison wrote:[color=green]
      > > 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_p tr<T> shared_ptr_mult ithread;
      > > typedef boost::shared_p tr<T> shared_ptr; // or something
      > > };
      > >
      > > int main()
      > > {
      > > Type::shared_pt r_multithread<M yClass> my_class_multit hreaded_ptr;
      > > Type::shared_pt r<MyClass> my_class_single threaded_ptr;
      > > }[/color]
      >
      > That should be written as:
      >
      > int main()
      > {
      > Type<MyClass>:: shared_ptr_mult ithread my_class_multit hreaded_ptr;
      > Type<MyClass>:: shared_ptr my_class_single threaded_ptr;
      > }
      >
      > shouldn't it?
      >[/color]

      Yes indeedy, thanks.

      john


      Comment

      • Richard van Wegen

        #4
        Re: Typedef of a template?

        > > > You cannot write template typedefs, its a limitiation of the C++ syntax[color=blue][color=green][color=darkred]
        > > > (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_p tr<T> shared_ptr_mult ithread;
        > > > typedef boost::shared_p tr<T> shared_ptr; // or something
        > > > };[/color]
        > >
        > > int main()
        > > {
        > > Type<MyClass>:: shared_ptr_mult ithread my_class_multit hreaded_ptr;
        > > Type<MyClass>:: shared_ptr my_class_single threaded_ptr;
        > > }[/color][/color]

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

        Thanks very much to you both for the help!

        Cheers
        Richard

        Comment

        Working...