473,473 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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_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
Jul 19 '05 #1
3 17153

"Richard van Wegen" <sp***********@volcanomail.com> wrote in message
news:c7**************************@posting.google.c 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
Jul 19 '05 #2

"Sarah Thompson" <sa***@telergy.remove.this.bit.com> wrote in message
news:be*******************@news.demon.co.uk...


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
Jul 19 '05 #3
> > > 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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arkadiy Vertleyb | last post by:
Hi all, I am having a problem trying to overload a function template, based on a typedef, such as: template<class T> struct A {}; template<class T>
5
by: Roger Leigh | last post by:
Although I've got over most of my template-related problems, I'm having trouble when I started to use default template parameters. For template type T, I've typedef'd this as object_type and then...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Patrick Kowalzick | last post by:
Hello NG, sorry to bother again, but I am a lit surprised that I got no answer on my post (attached below). So I refined the code a little bit :-). If there is a typedefed class X inside a...
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
5
by: jimmy | last post by:
I am trying to simulate typedef template similar to the suggestion of Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm However when implementing typedef templates according...
5
by: Alan Woodland | last post by:
Hi, I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
3
by: Adam Nielsen | last post by:
Hi everyone, Yet another syntax problem that's baffling me with templates. I want to instantiate a template with a single parameter as per normal, however the parameter is actually a template...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.