OT: C++ Template Functions

Václav Haisman V.Haisman at sh.cvut.cz
Thu Dec 27 13:36:06 PST 2007



Heiko Wundram (Beenic) wrote, On 27.12.2007 16:21:
> Hey all!
> 
> I'm currently trying to implement (and use) a C++ member function template, 
> but GCC won't eat the code I feed it.
> 
> The problem is most probably related to the fact that the group of member 
> functions is only discriminated by return type (i.e., the template parameter 
> defines the return type), not by parameter (which could be inferred).
> 
> From what I gather, the C++ spec should allow the following code to work (the 
> following is simplified from what I have, but pretty much the same 
> syntactically):
> 
> """
> template <typename U>
> class Test
> {
> 
> public:
> 
> 	template <typename V>
> 	V test()
> 		throw()
> 	{
> 		// Some stuff.
> 	}
> 
> };
> 
> template <typename U, typename V>
> void test2()
> 	throw()
> {
> 	Test<V>* x = new Test<V>();
> 	x->test<U>();
Change this to x->template test<U>(); For explanation see 14.2/4 of the year
2003 revision of the standard.

> 	delete x;
> }
> 
> int main(int argc, char** argv)
> {
> 	test2<int,short>();
> }
> """
> 
> gcc doesn't compile this code, no matter what I try to change the x->test<U> 
> expression to. It does compile the code if the type of x is not defined via a 
> template parameter in test2.
> 
> If this is against the specs, please tell me, otherwise, looking forward to 
> any hints on what may cause this!
> 

--
VH

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 250 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20071227/b90e2b3e/signature.pgp


More information about the freebsd-hackers mailing list