Template template parameters

E

eriwik

I'm quite new to template template programming and while trying to
write a function I discovered a small oddity, the following code
demonstrates this:

#include <iostream>

template<typename Val_, template<typename T = Val_> class Cont_> //
HERE
Val_ sum(Cont_<Val_>& c) {
Val_ sum = 0;
for (typename Cont_<Val_>::iterator i = c.begin(); i < c.end(); ++i)
sum += *i;
return sum;
}

template<typename T>
class Test {
T t[3];
public:
typedef T* iterator;

Test() { t[0] = t[1] = t[2] = 1; }
T* begin() {return t;}
T* end() {return t+3;}
};


int main() {
Test<int> test;
std::cout << sum(test);
}

If I change the word class to typename on the line commented HERE I get
compilation errors, but I seem to recall that when dealing with
templates class and typename are equivalent. Can someone explain this
to me?
 
K

kwikius

If I change the word class to typename on the line commented HERE I get
compilation errors, but I seem to recall that when dealing with
templates class and typename are equivalent. Can someone explain this
to me?

That is one of the few places where they arent interchangeable.

regards
Andy Little
 
I

IR

If I change the word class to typename on the line commented HERE
I get compilation errors, but I seem to recall that when dealing
with templates class and typename are equivalent. Can someone
explain this to me?

I recently ran into this one too. This syntax is explicitly required
by the standard in 14.1/1 (which Victor Bazarov pointed out to me at
that time).

I'm not sure whether there is a rationale behind that requirement, but
since I hang around there I'm (slowly) learning not to assume
anything. :)


Cheers,
 

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,062
Latest member
OrderKetozenseACV

Latest Threads

Top