g++ error: expected primary-expression

2019-09-03 17:41发布

look at this sample:

struct parent
{
    template <typename t>
    inline static t get_t();
};

struct child : public parent
{
    template <typename t>
    inline static t get_t()
    {
        return t(-1);
    }
};

template <typename t>
struct call
{
    inline static int get_value()
    {
        return t::get_t<int>();
    }
};

typedef call< child > test;

int main()
{
    int v = test::get_value();
}

The code compile with the following error:

In static member function 'static int call<t>::get_value()':
  error: expected primary-expression before 'int'
  error: expected ';' before 'int'
  error: expected unqualified-id before '>' token

When i compiled the code with visual c++ 2008 and Intel C++ it compiles without problem.

What that error mean?

2条回答
孤傲高冷的网名
2楼-- · 2019-09-03 18:17

just add

template

keyword:

template <typename t>
struct call
{
    inline static int get_value()
    {
        return t::template get_t<int>();
    }
};
查看更多
Fickle 薄情
3楼-- · 2019-09-03 18:22

You need the template qualifier:

return t::template get_t<int>();

See:

Where and why do I have to put the "template" and "typename" keywords?

查看更多
登录 后发表回答