By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,693 Members | 1,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,693 IT Pros & Developers. It's quick & easy.

template template parameters

P: n/a
I have this code from the book C++ Templates: The Complete Guide:

#ifndef ACCUM8_HPP_
#define ACCUM8_HPP_

#include "accumtraits4.hpp"
#include "sumpolicy2.hpp"

template <typename T,
template<typename,typenameclass Policy = SumPolicy,
typename Traits = AccumulationTraits<T
class Accum {
public:
typedef typename Traits::AccT AccT;
static AccT accum (T const* beg, T const* end) {

AccT total = Traits::zero();

while (beg != end) {
Policy<AccT,T>::accumulate(total, *beg);
++beg;
}
return total;
}
};

#endif /*ACCUM8_HPP_*/


Is it correct that it defines a class template with 3 template parameters:

1) typename T
2) template<typename,typenameclass Policy = SumPolicy
3) typename Traits = AccumulationTraits<T>
I read 2) as a template class "Policy" that has two unnamed template
parameters <typename,typename>.
In the while loop the line:

Policy<AccT,T>::accumulate(total, *beg);

AccT corresponds to the first "typename" in:

....
template<typename,typenameclass Policy = SumPolicy,
....

and "T" corresponds to the second.

Is this a correct interpretation?
May 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
desktop wrote:
I have this code from the book C++ Templates: The Complete Guide:

#ifndef ACCUM8_HPP_
#define ACCUM8_HPP_

#include "accumtraits4.hpp"
#include "sumpolicy2.hpp"

template <typename T,
template<typename,typenameclass Policy = SumPolicy,
typename Traits = AccumulationTraits<T
class Accum {
public:
typedef typename Traits::AccT AccT;
static AccT accum (T const* beg, T const* end) {

AccT total = Traits::zero();

while (beg != end) {
Policy<AccT,T>::accumulate(total, *beg);
++beg;
}
return total;
}
};

#endif /*ACCUM8_HPP_*/


Is it correct that it defines a class template with 3 template parameters:

1) typename T
2) template<typename,typenameclass Policy = SumPolicy
3) typename Traits = AccumulationTraits<T>
I read 2) as a template class "Policy" that has two unnamed template
parameters <typename,typename>.
In the while loop the line:

Policy<AccT,T>::accumulate(total, *beg);

AccT corresponds to the first "typename" in:

...
template<typename,typenameclass Policy = SumPolicy,
...

and "T" corresponds to the second.

Is this a correct interpretation?
Sounds about right.
May 2 '07 #2

P: n/a
desktop wrote:
I have this code from the book C++ Templates: The Complete Guide:

#ifndef ACCUM8_HPP_
#define ACCUM8_HPP_

#include "accumtraits4.hpp"
#include "sumpolicy2.hpp"

template <typename T,
template<typename,typenameclass Policy = SumPolicy,
typename Traits = AccumulationTraits<T
class Accum {
public:
typedef typename Traits::AccT AccT;
static AccT accum (T const* beg, T const* end) {

AccT total = Traits::zero();

while (beg != end) {
Policy<AccT,T>::accumulate(total, *beg);
++beg;
}
return total;
}
};

#endif /*ACCUM8_HPP_*/


Is it correct that it defines a class template with 3 template
parameters:
The term commonly used is "arguments", not "parameters", although
I've seen "parameters" used in place of "formal arguments"...
>
1) typename T
2) template<typename,typenameclass Policy = SumPolicy
3) typename Traits = AccumulationTraits<T>
Yes.
I read 2) as a template class "Policy" that has two unnamed template
parameters <typename,typename>.
The names of those arguments are not important (just like names of
formal arguments in a function declaration).
>

In the while loop the line:

Policy<AccT,T>::accumulate(total, *beg);

AccT corresponds to the first "typename" in:

...
template<typename,typenameclass Policy = SumPolicy,
...

and "T" corresponds to the second.

Is this a correct interpretation?
Yes. And 'AccT' is a typedef, it's a member type that comes from
'Traits', the third argument of 'Accum'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.