template template parameters

D

desktop

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,typename> class 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,typename> class 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,typename> class Policy = SumPolicy,
....

and "T" corresponds to the second.

Is this a correct interpretation?
 
N

Noah Roberts

desktop said:
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,typename> class 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,typename> class 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,typename> class Policy = SumPolicy,
...

and "T" corresponds to the second.

Is this a correct interpretation?

Sounds about right.
 
V

Victor Bazarov

desktop said:
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,typename> class 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,typename> class 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,typename> class 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
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top