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

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 that this is due to some interdependence, for example, the test code depends on the base class M, whose method is specialized inside Test. Is it correct?

+6
source share
1 answer

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

 M::template message<int>(); 

Otherwise, the compiler analyzes the code as if the value of M::message was a value that gives the following angle brackets of a different value (i.e., they are parsed as smaller, larger and larger than operators, and not as delimiters of template lists) . The compiler cannot recover from such improper parsing.

+7
source

Source: https://habr.com/ru/post/910720/


All Articles