Template template parameters #2

  • Follow


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 =3D Val_> class Cont_>  //
HERE
Val_ sum(Cont_<Val_>& c) {
	Val_ sum =3D 0;
	for (typename Cont_<Val_>::iterator i =3D c.begin(); i < c.end(); ++i)
		sum +=3D *i;
	return sum;
}

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

	Test() { t[0] =3D t[1] =3D t[2] =3D 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?

--
Erik Wikstr=F6m

0
Reply eriwik (500) 12/13/2006 12:12:32 PM

eriwik@student.chalmers.se wrote:

> 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

0
Reply andy199 (808) 12/13/2006 1:36:01 PM


eriwik@student.chalmers.se wrote:

> 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,
-- 
IR
0
Reply no_email8834 (66) 12/13/2006 7:22:12 PM

IR wrote:
> ...but since I hang around there I'm (slowly) learning not to assume
> anything. :)
> 
 
That's the very idea !

0
Reply reallyfyne (6) 12/13/2006 10:24:16 PM

3 Replies
28 Views

(page loaded in 0.072 seconds)


Reply: