=?windows-1252?Q?Re=3A_error=3A_expected_primary=2Dexpression_before_=91int?= =?windows-1252?Q?'?=
On 1 Nov., 16:58, raikanta.s...@gmail.com wrote:
Can somebody please explain why I am getting a compiler error when
compile the following block of code using g++ under Ubuntu 8.04? Thank
you.
template <typename T1>
class AA
{
public:
AA(){};
template <typename T2>
T2 const& get()
{
static T2 item;
return item;
}
private:
T1 x;
};
template <typename T1>
void testAA()
{
AA<T1> aa;
int i = aa.get<int>();
}
template <>
void testAA<int>()
{
AA<int> aa;
double d = aa.get<double>();
}
void test()
{
testAA<int>();
testAA<double>();
}
You need to insert the keyword 'template' at
the point where the primary function template
testAA() accesses get:
int i = aa.template get<int>();
This is necessary, because aa depends via
AA<T1> on the template parameter T1. Thus the
compiler cannot guarantee that get is a
(template) member function or not (A specialization
could define it as something different).
Under these conditions the occurrence of '<'
after get is interpreted as operator<, which
shortly later causes a conflict with 'int'.
This usage of 'template' is the correspondence of
the need to use 'typename' in other depending
expressions where the compiler cannot deduce whether
something is a type or not, e.g. in
template <class T>
std::vector<T>::iterator foo();
which needs to be adapted to
template <class T>
typename std::vector<T>::iterator foo();
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]