Archived

This topic is now archived and is closed to further replies.

jdhardy

Template syntax error

Recommended Posts

For some reason, this piece of code (simplified):
class Record
{
public:
    template<class value_t> value_t query(field f) const;
};

template<class value_t>
bool select_func(const Record &r, field f, value_t v)
{
    return(r.query<value_t>(f) == v); (*)
}
causes a "parse error before `>'' token" as well as a "parse error before `)'' token" in GCC 3.2. Changing value_t to be an actual type (int, float) causes the same error. If the template is removed from select_func and value_t typedef''d to int, however, there is no error. Anybody know how to fix this little problem?

Share this post


Link to post
Share on other sites
Try setting a template before the class keyword

template <class value_t> class Record{
public:
template<class value_t> value_t query(field f) const;
};


template<class value_t> bool select_func(const Record &r, field f, value_t v)
{
return(r.query(f) == v); (*)
}


BTW: How do you create this colored codebox?

[edited by - smeyer82 on January 18, 2004 2:48:22 PM]

Share this post


Link to post
Share on other sites
[source][/source] tags are your friend.

And I''m pretty sure this is a misfeature in GCC. Basically, GCC got super strict about putting in the template keyword inside of templated functions a few versions ago.

Change the bad line to:
return(r.template query<value_t>(f) == v);

(Compiles under 3.3.1, don''t have an 3.2 build lying around to test on.)

Share this post


Link to post
Share on other sites
To be honest, I''m not sure. The way I read the standard, I don''t think the template keyword should be required there, though it''s definately legal syntax. However, I do see where someone could think that the template keyword would be required. This why I called it a misfeature rather than a bug.

Either way, that extra keyword is both a pain in the rear and an eyesore. (In my opinion anyways.)

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
Either way, that extra keyword is both a pain in the rear and an eyesore. (In my opinion anyways.)
Agreed. It doesn''t look like a proper function call.

File it under ''stuff to know''.

Share this post


Link to post
Share on other sites
I think the standard is quite clear on the subject, it uses an example which is very similar to your code to illustrate the need of the extra template keyword.

quote:
14.2.4
When the name of a member template specialization appears after . or -­> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression
or qualified-id explicitly depends on a template-parameter
(14.6.2), the member template name must be prefixed by the keyword template.
Otherwise the name is assumed to name a nontemplate.
[Example:

class X {
public:
template<size_t> X* alloc();
template<size_t> static X* adjust();
};
template<class T> void f(T* p)
{
T* p1 = p->alloc<200>();
// illformed: < means less than

T* p2 = p->template alloc<200>();
// OK: < starts template argument list

T::adjust<100>();
// illformed:< means less than

T::template adjust<100>();
// OK: < starts explicit qualification

}

—end example]



Hope that clears it up



[edited by - Jingo on January 18, 2004 6:55:35 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
To be honest, I'm not sure. The way I read the standard, I don't think the template keyword should be required there, though it's definately legal syntax. However, I do see where someone could think that the template keyword would be required.


The .template is required to disambiguate the grammar - less-than vs. template argument list.

edit - hmm - Jingo already answered that...


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on January 18, 2004 7:38:27 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
The .template is required to disambiguate the grammar - less-than vs. template argument list.


Yes, but in ze_jackal''s case it is not required because neither r nor query are dependent names in the function. This is a gcc bug.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Yes, but in ze_jackal's case it is not required because neither r nor query are dependent names in the function. This is a gcc bug.


query does depend on the template parameter in his code. Which, IMHO, is exactly the case that 14.2.4 describes as needing an extra template keyword.

quote:

14.2.4
When the name of a member template specialization appears after . or -­> in a postfix-expression , or after nested-name-specifier in a qualified-id,
and the postfix-expression or qualified-id explicitly depends on a template-parameter(14.6.2), the member template name must be prefixed by the keyword template .



The name of the member template specialization in ze_jackal's code is 'query', which appears in a postfix-expression after a '.'. query depends on the template-parameter, and so the postfix-expression depends on the template-parameter, meaning the extra template keyword is needed.


[edited by - Jingo on January 19, 2004 5:39:50 AM]

Share this post


Link to post
Share on other sites
Ugh. One of many things they didn''t teach me in university.

Man, do I ever hate templates. Just never can seem to get the damn things to work. >_<

/me goes back to his Java.

Share this post


Link to post
Share on other sites