Discussion:
"expected primary-expression before '>'" error?
(too old to reply)
j***@yahoo.com
2008-04-18 23:19:51 UTC
Permalink
The following code gives an "expected primary-expression before '>'"
error when compiled with gcc, Dev-C++ version 4.9.9.2. There is no
such error with MSVC 2005 or 2008. I would be grateful to know if it a
gcc bug, or alternatively what is wrong with the code.

template< typename G >
struct Test
{
template< typename T > T f() const;
};

template< typename G, typename T >
void g()
{
Test< G > t;

t.f< T >(); // error reported for this line
}

void test()
{
g< float, int >();
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Matthew Collett
2008-04-19 07:39:24 UTC
Permalink
In article
Post by j***@yahoo.com
The following code gives an "expected primary-expression before '>'"
error when compiled with gcc, Dev-C++ version 4.9.9.2. There is no
such error with MSVC 2005 or 2008. I would be grateful to know if it a
gcc bug, or alternatively what is wrong with the code.
template< typename G >
struct Test
{
template< typename T > T f() const;
};
template< typename G, typename T >
void g()
{
Test< G > t;
t.f< T >(); // error reported for this line
}
You need

t.template f< T >();

The type of 't' depends on the template parameter 'G'. There is no
guarantee that in every specialisation of the template 'Test' the member
'f' will be a template. If the compiler is not told explicitly that 'f'
is in fact a template, it should assume that it is _not_, and parse '<'
as less-than rather than opening a template parameter.

Best wishes,
Matthew Collett
--
http://homepages.ihug.co.nz/~m_collett

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
a***@gmail.com
2008-04-19 07:39:17 UTC
Permalink
Post by j***@yahoo.com
The following code gives an "expected primary-expression before '>'"
error when compiled with gcc, Dev-C++ version 4.9.9.2. There is no
such error with MSVC 2005 or 2008. I would be grateful to know if it a
gcc bug, or alternatively what is wrong with the code.
gcc conforms to the standard and VC++ is behaving smarter than what
the standard requires.
Post by j***@yahoo.com
template< typename G >
struct Test
{
template< typename T > T f() const;
};
template< typename G, typename T >
void g()
{
Test< G > t;
t.f< T >(); // error reported for this line
}
Above, f depends on a template parameter. It can be a member variable,
an enum value, a member function, etc. of Test<G>. Unless told, the
compiler cannot know that f is a template itself.

So it parses the < character as "less than" and gets confused later
on.

You must tell the compiler that f is a template so that it parses < as
the opening bracket of a template parameter list. Sorry for the busy
syntax: :)

t.template f< T >();

Ali
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Thomas Maeder
2008-04-19 08:01:55 UTC
Permalink
Post by j***@yahoo.com
The following code gives an "expected primary-expression before '>'"
error when compiled with gcc, Dev-C++ version 4.9.9.2. There is no
such error with MSVC 2005 or 2008. I would be grateful to know if it
a gcc bug, or alternatively what is wrong with the code.
The code is wrong.
Post by j***@yahoo.com
template< typename G >
struct Test
{
template< typename T > T f() const;
};
template< typename G, typename T >
void g()
{
Test< G > t;
t.f< T >(); // error reported for this line
Since the name t is dependant, it's impossible for the compiler to
determine what the name t.f names. But the compiler needs this
information to determine whether < is the start of a template
parameter list or the less than operator.

The code therefore has to disambiguate the situation. The rule is that
unless the compiler is told otherwise, it has to assume that t.f does
not name a template. To tell the compiler otherwise, do

t.template f< T >();
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Loading...