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
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