Doubt in understanding template

B

Bharath

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 <classC> struct 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 ; }
template <class C> String <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
 
S

s_amrollahi

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 <classC> struct 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 ; }
template <class C> String <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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top