Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Typedef of a template?

Reply
Thread Tools

Typedef of a template?

 
 
Richard van Wegen
Guest
Posts: n/a
 
      07-14-2003
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-14-2003

"Richard van Wegen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-14-2003

"Sarah Thompson" <(E-Mail Removed)> wrote in message
news:betma4$n2m$1$(E-Mail Removed)...
>
>
> 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


 
Reply With Quote
 
Richard van Wegen
Guest
Posts: n/a
 
      07-15-2003
> > > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
java needs typedef Steve Green Java 11 03-25-2005 09:52 AM
How to remove dependency on a header file which has a typedef? Vikas C++ 1 08-01-2003 08:47 PM
template typedef as return type Robert A. T. Kaldy C++ 1 07-09-2003 06:25 PM
typedef enum qazmlp C++ 2 07-02-2003 11:55 AM



Advertisments