Template syntax error

Started by
11 comments, last by jdhardy 20 years, 3 months ago
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?
Advertisement
try using typename instead of class inside of the template

[edited by - nosfuratu on January 18, 2004 2:38:49 PM]
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]
[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.)
Thanks, SiCrane. Worked like a charm.

Is this part of The Standard, or a GCC extension?
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.)
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''.

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 thanT* p2 = p->template alloc<200>();// OK: < starts template argument listT::adjust<100>();// illformed:< means less thanT::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]
Well, at least it''s logical, but it''s not pretty. It''s unfortunate the template syntax isn''t cleaner, because templates are very useful.
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]
"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

This topic is closed to new replies.

Advertisement