problems instantiating a function template within another function template
Hi @,
please consider the following code:
#include <iostream>
template<class T> class X
{
public:
typedef T type;
operator type() { return t; }
X(T const& t0): t(t0) {}
private:
type t;
};
template<typename T> class A
{
public:
operator T() { return v; }
template<template <class> class OP>
typename OP<T>::type f() const { return OP<T>(v); }
private:
T v;
};
template<typename T> T g(A<T> const& x)
{
return x.f<X>(); // <-- syntax error
}
int main()
{
std::cout << A<int>().f<X>() << std::endl; // <-- here it's ok
std::cout << g(A<int>()) << std::endl;
}
Class X is a kind of operator class. It takes a value of 'T' in it's
constructors, performs some kind of computation of the result 'type'
(here omitted), stores the result in 't' until it's received using the
implicit conversion operator.
Class A offers an interface 'f' that applies an operator class like X to
it's internal values and returns the result.
Function g is supposed to use this interface but leads to the following
error:
$ g++-3.4 -Wall -o test test.cpp
test.cpp: In function `T g(const A<T>&)':
test.cpp:33: error: missing template arguments before '>' token
test.cpp:33: error: expected primary-expression before ')' token
The problem occurs only if g itself is a template. Does anyone know how
a function template can be instantiated within another function template?
Thanks,
Andreas
PS:
$ g++-3.4 -v
Reading specs from /usr/lib/gcc/i486-linux/3.4.4/specs
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr
--libexecdir=/usr/lib --with-gxx-include-dir=/usr/include/c++/3.4
--enable-shared --with-system-zlib --enable-nls
--without-included-gettext --program-suffix=-3.4 --enable-__cxa_atexit
--enable-libstdcxx-allocator=mt --enable-clocale=gnu
--enable-libstdcxx-debug --enable-java-gc=boehm --enable-java-awt=gtk
--disable-werror i486-linux
Thread model: posix
gcc version 3.4.4 20050314 (prerelease) (Debian 3.4.3-13)
$
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]