473,756 Members | 3,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templating a typedef or aliasing a template

Let say I have a class template Set which represent set of objects of
given type, and a struct template Pair representing an ordered pair,
ie:

#v+
template<class T>
class Set {
/* ... whatever ... */
public:
bool exists(const T &element) { /* ... */ }
Set<T&add (const T &element) { /* ... */ return *this; }
Set<T&remove(co nst T &element) { /* ... */ return *this; }
void union (const Set<T&set) { /* ... */ }
};

template<class T>
struct Pair {
T left, right;
};
#v-

Now, I want to create an "alias template" Relation<T(repr esenting a
binary relation) to mean Set< Pair<T. Unfortunately,
`template<class Ttypedef Set< Pair<T Relation<T>;' won't work, so
I had to define something like:

#v+
template<class Tclass Relation : public Set< Pair<T { };
#v-

but then add() and remove() methods would return reference to
Set<Pair<T which is not the same type as Relation<Tso I'd have to
create class template like:

#v+
template<class T>
class Relation {
Set< Pair<T set;
public:
bool exists(const Pair<T&pair) {
return set.exists(pair );
}
Relation<T&add (const Pair<T&pair) {
set.add(pair); return *this;
}
Relation<T&remo ve(const Pair<T&pair) {
set.remove(pair ); return *this;
}
void union (const Relation<T&rel) {
set.union(rel.s et);
}
};
#v-

ie. for each method from Set class I'd have to create method in
Relation class. It seems to me like a lot of useless code plus still
Relation<Tand Set< Pair<T are two different types so I wouldn't
be able to do:

#v+
Set< Pair<T set;
Relation<Trel;
set.union(rel);
#v-

unless I define an operator const Set< Pair<T() and that's just
another piece of useless code.

Is then any nice and easy way to create a nice alias so user wouldn't
have to type Set< Pair<T but rather Relation<T>?

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Nov 12 '06 #1
7 1453

Michal Nazarewicz wrote:
Let say I have a class template Set which represent set of objects of
given type, and a struct template Pair representing an ordered pair,
ie:

#v+
template<class T>
class Set {
/* ... whatever ... */
public:
bool exists(const T &element) { /* ... */ }
Set<T&add (const T &element) { /* ... */ return *this; }
Set<T&remove(co nst T &element) { /* ... */ return *this; }
void union (const Set<T&set) { /* ... */ }
};

template<class T>
struct Pair {
T left, right;
};
#v-

Now, I want to create an "alias template" Relation<T(repr esenting a
binary relation) to mean Set< Pair<T. Unfortunately,
`template<class Ttypedef Set< Pair<T Relation<T>;' won't work, so
I had to define something like:

#v+
template<class Tclass Relation : public Set< Pair<T { };
#v-

but then add() and remove() methods would return reference to
Set<Pair<T which is not the same type as Relation<Tso I'd have to
create class template like:

#v+
template<class T>
class Relation {
Set< Pair<T set;
public:
bool exists(const Pair<T&pair) {
return set.exists(pair );
}
Relation<T&add (const Pair<T&pair) {
set.add(pair); return *this;
}
Relation<T&remo ve(const Pair<T&pair) {
set.remove(pair ); return *this;
}
void union (const Relation<T&rel) {
set.union(rel.s et);
}
};
#v-

ie. for each method from Set class I'd have to create method in
Relation class. It seems to me like a lot of useless code plus still
Relation<Tand Set< Pair<T are two different types so I wouldn't
be able to do:

#v+
Set< Pair<T set;
Relation<Trel;
set.union(rel);
#v-

unless I define an operator const Set< Pair<T() and that's just
another piece of useless code.

Is then any nice and easy way to create a nice alias so user wouldn't
have to type Set< Pair<T but rather Relation<T>?


Hi,
Maybe I am missing something, but I don't see anythin wrong woth your
first proposed method.

template<class Tclass Relation : public Set< Pair<T { };

For all intent an purposes this derived class will act exactly like its
Base, theres no difference, well almost. The only time I think you will
have an issue is any function that takes by value or retuns by value
Set<Pair<T. I use this method for typedefing templates and never had
a problem with it. When referring by pointer or reference there is no
difference, well none that I can see.

N

Nov 12 '06 #2

Ni*****@yahoo.c o.uk wrote:
Michal Nazarewicz wrote:
Let say I have a class template Set which represent set of objects of
given type, and a struct template Pair representing an ordered pair,
ie:

#v+
template<class T>
class Set {
/* ... whatever ... */
public:
bool exists(const T &element) { /* ... */ }
Set<T&add (const T &element) { /* ... */ return *this; }
Set<T&remove(co nst T &element) { /* ... */ return *this; }
void union (const Set<T&set) { /* ... */ }
};

template<class T>
struct Pair {
T left, right;
};
#v-

Now, I want to create an "alias template" Relation<T(repr esenting a
binary relation) to mean Set< Pair<T. Unfortunately,
`template<class Ttypedef Set< Pair<T Relation<T>;' won't work, so
I had to define something like:

#v+
template<class Tclass Relation : public Set< Pair<T { };
#v-

but then add() and remove() methods would return reference to
Set<Pair<T which is not the same type as Relation<Tso I'd have to
create class template like:

#v+
template<class T>
class Relation {
Set< Pair<T set;
public:
bool exists(const Pair<T&pair) {
return set.exists(pair );
}
Relation<T&add (const Pair<T&pair) {
set.add(pair); return *this;
}
Relation<T&remo ve(const Pair<T&pair) {
set.remove(pair ); return *this;
}
void union (const Relation<T&rel) {
set.union(rel.s et);
}
};
#v-

ie. for each method from Set class I'd have to create method in
Relation class. It seems to me like a lot of useless code plus still
Relation<Tand Set< Pair<T are two different types so I wouldn't
be able to do:

#v+
Set< Pair<T set;
Relation<Trel;
set.union(rel);
#v-

unless I define an operator const Set< Pair<T() and that's just
another piece of useless code.

Is then any nice and easy way to create a nice alias so user wouldn't
have to type Set< Pair<T but rather Relation<T>?

Hi,
Maybe I am missing something, but I don't see anythin wrong woth your
first proposed method.

template<class Tclass Relation : public Set< Pair<T { };

For all intent an purposes this derived class will act exactly like its
Base, theres no difference, well almost. The only time I think you will
have an issue is any function that takes by value or retuns by value
Set<Pair<T. I use this method for typedefing templates and never had
a problem with it. When referring by pointer or reference there is no
difference, well none that I can see.
PS ... what I will add, you didn't have anything but the compiler
provided default and copy constructors and the compiler provided
assignement operator in you Set class. If this was not the case you
would have to provide your derived class with the neccessary
constructors and the assignment operator is not inheritable.

Nov 12 '06 #3

Michal Nazarewicz wrote:
Let say I have a class template Set which represent set of objects of
given type, and a struct template Pair representing an ordered pair,
ie:
you basically want a template typedef?

if so:

template<class T>
struct Relation_typede f
{
typedef Set< Pair<T Relation;
};

and use it as Relation_typede f<real type>::Relation
instead of Set<Pair<real type

Nov 13 '06 #4
Michal Nazarewicz wrote:
>Let say I have a class template Set which represent set of objects of
given type, and a struct template Pair representing an ordered pair,
ie:

#v+
template<cla ss T>
class Set {
/* ... whatever ... */
public:
bool exists(const T &element) { /* ... */ }
Set<T&add (const T &element) { /* ... */ return *this; }
Set<T&remove(co nst T &element) { /* ... */ return *this; }
void union (const Set<T&set) { /* ... */ }
};

template<cla ss T>
struct Pair { T left, right; };
#v-

Now, I want to create an "alias template" Relation<T(repr esenting a
binary relation) to mean Set< Pair<T.
[...]

Ni*****@yahoo.c o.uk writes:
Maybe I am missing something, but I don't see anythin wrong woth your
first proposed method.

template<class Tclass Relation : public Set< Pair<T { };

For all intent an purposes this derived class will act exactly like its
Base, theres no difference, well almost. The only time I think you will
have an issue is any function that takes by value or retuns by value
Set<Pair<T.
Ah, and that's my concern. For instance, operator+() returns
Set<Pair<T by value and I think that it could matter sometimes. Or
am I wrong and worry too much?

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Nov 13 '06 #5
"dasjotre" <da******@googl email.comwrites :
you basically want a template typedef?

if so:

template<class T>
struct Relation_typede f
{
typedef Set< Pair<T Relation;
};

and use it as Relation_typede f<real type>::Relation
instead of Set<Pair<real type
The way I see it, idea of aliases is to make names shorter and easier
to read. ;)

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Nov 13 '06 #6
Michal Nazarewicz wrote:
"dasjotre" <da******@googl email.comwrites :
>you basically want a template typedef?

if so:

template<cla ss T>
struct Relation_typede f
{
typedef Set< Pair<T Relation;
};

and use it as Relation_typede f<real type>::Relation
instead of Set<Pair<real type

The way I see it, idea of aliases is to make names shorter and easier
to read. ;)
Well, sure. But not necessarily at this level of infrastructure. Once
you have a name for type you're dealing with you write a typedef:

typedef Relation_typede f<foo>::Relatio n foo_rel;

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Nov 13 '06 #7

Michal Nazarewicz wrote:
"dasjotre" <da******@googl email.comwrites :
you basically want a template typedef?

if so:

template<class T>
struct Relation_typede f
{
typedef Set< Pair<T Relation;
};

and use it as Relation_typede f<real type>::Relation
instead of Set<Pair<real type

The way I see it, idea of aliases is to make names shorter and easier
to read. ;)
If you like to know why, go to comp.std.c++ and search
for 'template typedefs'

Nov 14 '06 #8

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

Similar topics

31
2090
by: Leif K-Brooks | last post by:
I'm planning to start on a fairly large web application, most likely using mod_python. I recently wrote a fairly small (but real-world useful) web app with it, and all of those req.write()s made for really ugly code. Would a templating engine solve that? Does anyone have any suggestions about which one to use?
7
1261
by: Ksenia Marasanova | last post by:
Hi, I am looking for fast, simple templating system that will allow me to do the following: - embed Python code (or some templating code) in the template - generate text output (not only XML/HTML) I don't need a framework (already have it), but just simple templating. The syntax I had in mind is something like that:
2
1578
by: lloyd christopher | last post by:
im developing a template based web application in perl and need some guidance. currently its all pretty simple stuff, we tell the designers place holders to use and then &username; or %USERNAME% or <MYusername/> would be replaced by the value. basically what the perl cookbook uses in a few examples iirc. however more and more we have to deal with more complex data. variable length lists of data. say for example a list of new products...
7
2463
by: Dennis Myrén | last post by:
Hi. Is there any way to define an alias for a type like you could in C++: typedef int NUMBER; I tried using #define but that did not work. Thank you. -- Regards,
18
4972
by: pkassianidis | last post by:
Hello everybody, I am in the process of writing my very first web application in Python, and I need a way to generate dynamic HTML pages with data from a database. I have to say I am overwhelmed by the plethora of different frameworks, templating engines, HTML generation tools etc that exist. After some thought I decided to leave the various frameworks aside for the
4
1639
by: newbie | last post by:
Say I have the following class: class MyAbs { virtual ~MyAbs() {} virtual void foo() = 0; virtual void bar() {cout << "abs::bar"; } } class MyDer1: public MyAbs { MyDer1() {counter = 0; }
9
1409
by: MathStuf | last post by:
I am working on a class that will be a matrix based on a vector of vectors. I am having trouble with the following code causing errors on compilation: template<class Tclass MatrixBase { public: MatrixBase() { width = 0;
2
1141
by: alan | last post by:
Hello all, in a project I have I have an abstract base type with some virtual functions: class Base{ public: virtual Base* t1() =0; virtual Base* t2() =0; } I have a bunch of derived classes, much like the following:
10
1719
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming constructs with the HTML document itself. An opposite approach to this form of dynamic HTML production is called push-style templating, as coined by Terence Parr:
0
9456
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
10034
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
9872
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...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
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();...
3
2666
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.