Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange GCC error: expected primary-expression before ',' token

Tags:

c++

gcc

templates

I'm still trying to migrate from MSVC to GCC, but I can't seem to find a solution to the following problem:

template < typename A, typename B, typename C, typename D >
class Test
{
public:
    Test (B* pObj, C fn, const D& args) : _pObj(pObj), _fn(fn), _args(args)
    {
    }

    A operator() ()
    {
        return _args.operator() < A, B, C > (_pObj, _fn); // error: expected primary-expression before ',' token
    }

    B* _pObj;
    C _fn;
    D _args;
};

Please help!

like image 258
Ryan Avatar asked Apr 27 '11 09:04

Ryan


People also ask

What does expected primary expression before token mean?

This typically means that your code is missing a closing '}'. For every opening '{' there should be a closing '}' expected primary-expression before ')' token. Sometimes this happens when the variable passed into a function isn't the type the function expected.

What is primary expression in C++?

Primary expressions are the building blocks of more complex expressions. They may be literals, names, and names qualified by the scope-resolution operator ( :: ). A primary expression may have any of the following forms: primary-expression.


1 Answers

Try return _args.template operator() < A, B, C > (_pObj, _fn);.

Without the template keyword the parse would be different. Without that extra use of template, the compiler does not know that the less-than token (<) that follows is not really "less than" but the beginning of a template argument list.

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 non-template.

P.S: Read this Stackoverflow FAQ Entry

like image 143
Prasoon Saurav Avatar answered Nov 09 '22 23:11

Prasoon Saurav