473,762 Members | 6,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

one more template question and some typedef

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[1];
statement 3: Array<int>::ele ment_type y = x[1];
but I can't understand the last one which is
Array<int>::ele ment_type y = x[1];
We have a typedef T element_type; in the template Array class see below
and in my example I use int as the type parameter for T so
that would mean that it says
Array<int>::int y = x[1];
this is even more confusing for me.
If the Array template class for example had a static variable with name
number then you would
use Array<int>::num ber; to access this static variable.

template <typename T>
class Array
{
public:
Array(size_t=10 0); // size_t is predefined
Array(const Array<T>&);
Array<T>& operator=(const Array<T>&);
virtual ~Array();
virtual const T& operator[](int) const;
virtual T& operator[](int);
int size() const;

typedef T element_type;
protected:
size_t size_;
T* array;
};

Many thanks

//Tony

Aug 15 '05 #1
7 2147
Tony Johansson wrote:
I have the following Array template class see below.
I execute these three statements
statement 1: Array<int> x(5);
statement 2: cin >>x[1];
statement 3: Array<int>::ele ment_type y = x[1];
but I can't understand the last one which is
Array<int>::ele ment_type y = x[1];
We have a typedef T element_type; in the template Array class see below
and in my example I use int as the type parameter for T so
that would mean that it says
Array<int>::int y = x[1];
this is even more confusing for me.
No, it doesn't mean it says "Array<int>::in t y = x[1];".
It means what it says,

Array<int>::ele ment_type y = x[1];

or you can rewrite it to say

int y = x[1];

'element_type' does not mean 'int' by itself. It only means 'int' if
used _along_ with the class in which it's defined. For example, in
Array<string>, 'element_type' would mean 'string'. So, if you write

Array<string>:: element_type

it would be the same as writing

string
If the Array template class for example had a static variable with name
number then you would
use Array<int>::num ber; to access this static variable.


Yes.

V
Aug 15 '05 #2

"Victor Bazarov" <v.********@com Acast.net> skrev i meddelandet
news:aV******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
Tony Johansson wrote:
I have the following Array template class see below.
I execute these three statements
statement 1: Array<int> x(5);
statement 2: cin >>x[1];
statement 3: Array<int>::ele ment_type y = x[1];
but I can't understand the last one which is
Array<int>::ele ment_type y = x[1];
We have a typedef T element_type; in the template Array class see below
and in my example I use int as the type parameter for T so
that would mean that it says
Array<int>::int y = x[1];
this is even more confusing for me.


No, it doesn't mean it says "Array<int>::in t y = x[1];".
It means what it says,

Array<int>::ele ment_type y = x[1];

or you can rewrite it to say

int y = x[1];

'element_type' does not mean 'int' by itself. It only means 'int' if
used _along_ with the class in which it's defined. For example, in
Array<string>, 'element_type' would mean 'string'. So, if you write

Array<string>:: element_type

it would be the same as writing

string
If the Array template class for example had a static variable with name
number then you would
use Array<int>::num ber; to access this static variable.


Yes.

V


I can't understand how Array<int>::ele ment_type
is the same as int in my example.
or that Array<string>:: element_type is the same as string
Some question that might help me to understand this.
How does the scope operation be understood here?
What meaning have T in the typedef T element_type;?
I want to understand the scope operatorn in such way that there is some
variable
in the Array template class that has name element_type.
The fully name of the Array template class when you have int as the
parameter is
Array<int>
Can you give me some more explanation about Array<int>::ele ment_type
that might help me to understand this.

//Tony
Aug 15 '05 #3
Tony wrote:
"Victor Bazarov" <v.********@com Acast.net> skrev i meddelandet
news:aV******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
Tony Johansson wrote:
I have the following Array template class see below.
I execute these three statements
statement 1: Array<int> x(5);
statement 2: cin >>x[1];
statement 3: Array<int>::ele ment_type y = x[1];
but I can't understand the last one which is
Array<int>:: element_type y = x[1];
We have a typedef T element_type; in the template Array class see below
and in my example I use int as the type parameter for T so
that would mean that it says
Array<int>:: int y = x[1];
this is even more confusing for me.
No, it doesn't mean it says "Array<int>::in t y = x[1];".
It means what it says,

Array<int>::ele ment_type y = x[1];

or you can rewrite it to say

int y = x[1];

'element_type ' does not mean 'int' by itself. It only means 'int' if
used _along_ with the class in which it's defined. For example, in
Array<string> , 'element_type' would mean 'string'. So, if you write

Array<string>:: element_type

it would be the same as writing

string

If the Array template class for example had a static variable with name
number then you would
use Array<int>::num ber; to access this static variable.


Yes.

V

I can't understand how Array<int>::ele ment_type
is the same as int in my example.


Try a simple substitution for this struct template:

template<class T> struct A { typedef T type; };

If I instantiate A<int>, what happens? It's as if I declare a type

struct A<int> /* incorrect syntax, but will do */ {
typedef int type;
};

(anywhere 'T' was used inside the 'A' definition, I put 'int'). Now, what
does 'A<int>::type' stand for? It's typedef'ed to be the same as 'int'.
So, anywhere I want to use 'int' I _can_ use 'A<int>::type' because they
are defined to mean the same thing.
or that Array<string>:: element_type is the same as string
Some question that might help me to understand this.
How does the scope operation be understood here?
It's not "the scope operation". It's what you may call a "class-wide
member access operation". A typedef inside a class is a class-wide
member. It has a name which you can access using the '::'. The name
of that member in my example is 'type'. A way to access it is to supply
the name of the _class_ with the scope resolution operator:

A<int> :: type
^^^^^^ ^^ ^^^^
Name of the class Scope resolution Member name
What meaning have T in the typedef T element_type;?
Just like with any other typedef, 'element_type' is now the synonym for
whatever 'T' is. For any particular instantiation of the template, 'T'
means something specific. So, in any particular instantiation of your
'Array' template, 'element_type' will mean something in particular.
I want to understand the scope operatorn in such way that there is some
variable
in the Array template class that has name element_type.
I don't understand the sentence above.
The fully name of the Array template class when you have int as the
parameter is
Array<int>
It's called "instantiation" , not "fully name of the Array template class".
Can you give me some more explanation about Array<int>::ele ment_type
that might help me to understand this.


See above. And, yes, I can give you more if you ask more specific
questions.

V
Aug 15 '05 #4

"Victor Bazarov" <v.********@com Acast.net> skrev i meddelandet
news:bl******** **********@news read1.mlpsca01. us.to.verio.net ...
Tony wrote:
"Victor Bazarov" <v.********@com Acast.net> skrev i meddelandet
news:aV******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
Tony Johansson wrote:

I have the following Array template class see below.
I execute these three statements
statement 1: Array<int> x(5);
statement 2: cin >>x[1];
statement 3: Array<int>::ele ment_type y = x[1];
but I can't understand the last one which is
Array<int>: :element_type y = x[1];
We have a typedef T element_type; in the template Array class see below
and in my example I use int as the type parameter for T so
that would mean that it says
Array<int>: :int y = x[1];
this is even more confusing for me.

No, it doesn't mean it says "Array<int>::in t y = x[1];".
It means what it says,

Array<int>::ele ment_type y = x[1];

or you can rewrite it to say

int y = x[1];

'element_typ e' does not mean 'int' by itself. It only means 'int' if
used _along_ with the class in which it's defined. For example, in
Array<string >, 'element_type' would mean 'string'. So, if you write

Array<string>:: element_type

it would be the same as writing

string
If the Array template class for example had a static variable with name
number then you would
use Array<int>::num ber; to access this static variable.

Yes.

V

I can't understand how Array<int>::ele ment_type
is the same as int in my example.


Try a simple substitution for this struct template:

template<class T> struct A { typedef T type; };

If I instantiate A<int>, what happens? It's as if I declare a type

struct A<int> /* incorrect syntax, but will do */ {
typedef int type;
};

(anywhere 'T' was used inside the 'A' definition, I put 'int'). Now, what
does 'A<int>::type' stand for? It's typedef'ed to be the same as 'int'.
So, anywhere I want to use 'int' I _can_ use 'A<int>::type' because they
are defined to mean the same thing.
or that Array<string>:: element_type is the same as string
Some question that might help me to understand this.
How does the scope operation be understood here?


It's not "the scope operation". It's what you may call a "class-wide
member access operation". A typedef inside a class is a class-wide
member. It has a name which you can access using the '::'. The name
of that member in my example is 'type'. A way to access it is to supply
the name of the _class_ with the scope resolution operator:

A<int> :: type
^^^^^^ ^^ ^^^^
Name of the class Scope resolution Member name
What meaning have T in the typedef T element_type;?


Just like with any other typedef, 'element_type' is now the synonym for
whatever 'T' is. For any particular instantiation of the template, 'T'
means something specific. So, in any particular instantiation of your
'Array' template, 'element_type' will mean something in particular.
I want to understand the scope operatorn in such way that there is some
variable
in the Array template class that has name element_type.


I don't understand the sentence above.
The fully name of the Array template class when you have int as the
parameter is
Array<int>


It's called "instantiation" , not "fully name of the Array template class".
Can you give me some more explanation about Array<int>::ele ment_type
that might help me to understand this.


See above. And, yes, I can give you more if you ask more specific
questions.

V


I think I start to understand it now. I just check with you Victor hope you
read this.
Assume there exist a valid type x that I use to get a specialisation of the
template Array.
If you instantiate the template class Array and pass type x as parameter you
get the instantiation name Array<x>.
If I want to access a class-wide member in the Array template I use the ::
In my case I have a class-wide member with name element_type so I access
this element_type by giving statement
Array<x>::eleme nt_type
this whole expession can always be substituted with the type that this
template was called with in my example here it's x because we have this
typedef T element_type

Does my thinking seems to be about correct?

//Tony
Aug 15 '05 #5
Tony Johansson wrote:
[..]
Assume there exist a valid type x that I use to get a specialisation of the
template Array.
If you instantiate the template class Array and pass type x as parameter you
get the instantiation name Array<x>.
If I want to access a class-wide member in the Array template I use the ::
In my case I have a class-wide member with name element_type so I access
this element_type by giving statement
Array<x>::eleme nt_type
this whole expession can always be substituted with the type that this
template was called with in my example here it's x because we have this
typedef T element_type

Does my thinking seems to be about correct?


Yes... I am trying to find some kind of hidden pothole in that or
some additional condition that has to be satisfied, but it all seems
to be fine.

I recommend looking at the standard library containers and how they
define 'value_type' members. That's a common requirement for all of
them, and you should be able to rely on that, for example.

V
Aug 15 '05 #6
"Array<int>::el ement_type" is really "typename
Array<int>::ele ment_type"

and in fact the newer compilers issue warnings asking you to write it
this way.

It is a hint to the compiler to look for a type definition rather than
a data/function member.

Aug 16 '05 #7
neerajk wrote:
"Array<int>::el ement_type" is really "typename
Array<int>::ele ment_type"

and in fact the newer compilers issue warnings asking you to write it
this way.


Really? Which ones? Please post a complete compilable piece of code
that illustrates what you're talking about.

V
Aug 16 '05 #8

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

Similar topics

6
3353
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something like that possible? Some workaround? Thank you, Patrick
0
1876
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up zip. Anyhow, after much trial and error I came up with this. This "contains_member" template sets value to 1 if the type D contains a typedef named MEMBER of type TD.
31
3534
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x : y;
5
1527
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One thing I don't like about the way the current policy template is setup is that for the ptr_policy class the first template type is different from the template type given to the policy. And on the other policy classes, the template type is the same....
6
1582
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each B can be child to zero, one, or more A's. I just call that "A is a parent of B", and "B is a child of A". The same goes for B and C and for C and D. So A is only a parent, B and C are both parents and children, and D is only a child.
1
2360
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one single class. Instead I have to separate implementation for binary and ascii in two different classes as I don't know how to use the traits technique for constructors. Any pointers very welcome !
2
3343
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template class B has an instance of template class A, which has some base type T (usually int or double). However, the base type T is important to calculations in B, so I would like to obtain access to the type for further variable declaration within B. ...
19
7934
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
4
2318
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef Tparent::Tptr Tptr; Consider example:
0
9554
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
9377
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
10136
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...
1
9925
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
9811
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
6640
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
5266
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...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
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.