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