Templates: template function not playing well with class's template member function

前端 未结 2 1131
渐次进展
渐次进展 2020-12-02 21:14

This is a minimal test case of some code that I actually have. It fails when it tries to evaluate a.getResult():

test.cpp: In function          


        
2条回答
  •  孤街浪徒
    2020-12-02 21:29

    When you refer to a template that is a member of dependent type, you have to prepend it with a keyword template. This is how the call to getResult inside printStuff should look

    size_t value = a.template getResult();
    

    This is similar to using the keyword typename when referring to nested typenames in a dependent type. For some reason, the bit about typename with nested types is rather well-known, but the similar requirement for template with nested templates is relatively unknown.

    Note that the general syntax structure is a bit different though. The typename is always put in front of the full name of the type, while template is inserted in the middle.

    Again, this is only necessary when you are accessing a template member of a dependent type, which in the above example would be A in printStuff. When you call foo.getResult<> in main the type of foo is not dependent, so there's no need to include the template keyword.

提交回复
热议问题