Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call a templated method of a template class that is derived from

Tags:

c++

templates

struct Messages
{
      template <typename V>
      static const char* message() {return "test mesage";}
};

template <int Min, class M=Messages>
struct Test: public M
{
    Test() 
    {
        M::message<int>(); //error: expected primary-expression before 'int'
    }
};

int main()
{
     Test<5, Messages> t;
}

I suspect this has to do with some mutual dependency, like the code of Test depends on the base class M whose method is specialized inside Test. Is this correct?

like image 403
Alexander Vassilev Avatar asked Mar 14 '12 15:03

Alexander Vassilev


Video Answer


1 Answers

M::message is a dependent name since M is a template argument. The compiler cannot know that a dependent name is itself a template, therefore you need to specify this explicitly:

M::template message<int>();

Otherwise the compiler parses the code as though M::message were a value, which gives following angle brackets a different meaning (i.e. they are parsed as smaller-than and greater-than operators and not as template list delimiters). The compiler cannot recover from such a wrong parse.

like image 200
Konrad Rudolph Avatar answered Sep 29 '22 21:09

Konrad Rudolph