473,597 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ 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_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
Jul 19 '05 #1
3 17164

"Richard van Wegen" <sp***********@ volcanomail.com > wrote in message
news:c7******** *************** ***@posting.goo gle.com...
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


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

"Sarah Thompson" <sa***@telergy. remove.this.bit .com> wrote in message
news:be******** ***********@new s.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_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;
}


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?


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


int main()
{
Type<MyClass>:: shared_ptr_mult ithread my_class_multit hreaded_ptr;
Type<MyClass>:: shared_ptr my_class_single threaded_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
7100
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
3003
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 typedef'd std::vector<T> menu_list. This doesn't seem to work though: 40 template<typename W> 41 class ObjectOptionMenuDescribeObject 42 { 43 public:
7
2138
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 y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array class see below
2
2339
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 line is very long. I'm wondering if there is any way to suppress it. I can only think of typedef. But I'm not sure whether I can use typedef for the return type. Would you please help me? Please don't be daunted by the length of the code.
2
2190
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 class Y which has the same typedef name and used in the form "Instance_Y::type::type" MSVC7.1 fails to compile. See example below.
6
7300
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 (condition) typedef char T; #else typedef short T;
5
2427
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 to his suggestion, I run into type inference problems when passing typedef templates as arguments to template functions. It seems the compiler (gcc 4.0) cannot resolve the typedef template with an instantiation of that typedef template. Below is...
5
2126
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 produce behavior, using standard C++, akin to what I guess can be best called a "typedef specializations". I.e. this being the default:
12
2311
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 <iostream> using namespace std; template <class ClassB> class ClassA
3
1842
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 class itself, with all *its* parameters filled out (in the form of a typedef.) I can't work out how to break apart the typedef to reveal what data types were used to create it in the first place. Here is some example code that demonstrates the...
0
7969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7886
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5431
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.