473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Doubt in understanding template

Hello everyone,
I was going through templates section at Bjarne Stroustrup's "C++
Programming Language" (13.2.1 Defining a Template
[temp.string.details]). I couldn't understand some part of it. Need
your help in understanding.

There is an e.g. given in this section (copy pasted below)

************************************************** ***************************************
template <classCstruct String <C>:: S r e p {
C *s ; // pointer to elements
int sz ; // number of elements
int n ; // reference count
/ / ...
};
template <class C>C String <C>:: read (int i ) const { return rep >
s [i]; }
template <class CString <C>:: String ()
{
p = new Srep (0 ,C());
}

Template parameters, such as C , are parameters rather than names of
types defined externally to the
template. However, that doesn't affect the way we write the template
code using them. Within the
scope of S t r i n g <C >, qualification with <C is redundant for
the name of the template itself, so S t r i n g <C >:: S t r i n g is
the name for the constructor. If you prefer, you can be explicit:
t e m p l a t e <c l a s s C S t r i n g <C >:: S t r i n g <C >()
{
p = n e w S r e p (0 ,C ());
}

************************************************** ***************************************
I understand that scope of S t r i n g <C is till end of the
declaration prefixed by t e m p l a t e <c l a s s C >.

What I don't understand here is about qualification with <C ? What
is that? and how it is redundant to name of the template?

- Bharath

Jun 23 '07 #1
1 1143
On Jun 23, 7:21 pm, Bharath <bharath.donni...@gmail.comwrote:
Hello everyone,
I was going through templates section at Bjarne Stroustrup's "C++
Programming Language" (13.2.1 Defining a Template
[temp.string.details]). I couldn't understand some part of it. Need
your help in understanding.

There is an e.g. given in this section (copy pasted below)

************************************************** ****************************************
template <classCstruct String <C>:: S r e p {
C *s ; // pointer to elements
int sz ; // number of elements
int n ; // reference count
/ / ...};

template <class C>C String <C>:: read (int i ) const { return rep >
s [i]; }
template <class CString <C>:: String ()
{
p = new Srep (0 ,C());

}

Template parameters, such as C , are parameters rather than names of
types defined externally to the
template. However, that doesn't affect the way we write the template
code using them. Within the
scope of S t r i n g <C >, qualification with <C is redundant for
the name of the template itself, so S t r i n g <C >:: S t r i n g is
the name for the constructor. If you prefer, you can be explicit:
t e m p l a t e <c l a s s C S t r i n g <C >:: S t r i n g <C >()
{
p = n e w S r e p (0 ,C ());

}

************************************************** ****************************************
According to the rules of C++ anything before :: is out of the scope
of in this case calss String. So before :: We should use template
parameter, but inside the calss scope, I mean after ::, no need for
using qualification.
Regards,
S. Amrollahi
I understand that scope of S t r i n g <C is till end of the
declaration prefixed by t e m p l a t e <c l a s s C >.

What I don't understand here is about qualification with <C ? What
is that? and how it is redundant to name of the template?

- Bharath

Jun 23 '07 #2

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

Similar topics

1
by: Babu | last post by:
Hi, I am a Perl newbie and have a doubt on Perl exception handling. My understanding regarding exception handling is execute a piece of code, if any exception occurs, handle the exception and...
8
by: Gerald Aichholzer | last post by:
Hello NG, I try to apply the following template <xsl:template match="objectgroup"> <xsl:copy> <xsl:apply-templates select="*|@*"/> <xsl:variable name="object" select="."/> <xsl:for-each...
1
by: SK | last post by:
Hi all, I have a doubt in C++ Templates by Nicolai M. Josuttis. On Page 17 there is a line "In general, it is a good idea not to change more than necessary when overloading function templates....
3
by: Old Monk | last post by:
Hi all, I have got a doubt in Koenig& Moo's AC++. On page 228 and 230, authors provide definitions for Core and Grad classes. Grad IS-A Core, i.e. public inheritance. What puzzles me is that...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
1
by: Siva sakthi via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Siva sakthi Hi, I have populated the data from database in aspx page using template column.
5
by: edu.mvk | last post by:
Hi Friends, I have subscribed to this group recently. I have a doubt in templates in C++ A fraction of code given in "The C++ Programming Language" -By B.Stroustrup is as follows : ...
2
by: ketan | last post by:
Hi, While reading the Boost::shared_ptr implementation, I came across this method template<class Tclass shared_ptr { ..... template<class Y> shared_ptr(shared_ptr<Yconst & r,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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,...

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.