1

My attempt is:

template<typename Derived>
struct Base
{
    void A()
    {
        ((Derived *)this)->B<42>();
    }
};

struct Derived : Base<Derived>
{
    template<int> void B() { }
};

(http://coliru.stacked-crooked.com/a/cb24dd811b562466)

Which results in

main.cpp: In member function 'void Base<Derived>::A()':
main.cpp:6:34: error: expected primary-expression before ')' token
         ((Derived *)this)->B<42>();
                                  ^
main.cpp: In instantiation of 'void Base<Derived>::A() [with Derived = Derived]':
main.cpp:17:17:   required from here
main.cpp:6:30: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
      ((Derived *)this)->B<42>();
      ~~~~~~~~~~~~~~~~~~~~^~~
Dani
  • 27,760
  • 14
  • 76
  • 131

1 Answers1

2

You need the keyword template for calling the template function on the dependent type:

((Derived *)this)->template B<42>();
//                 ~~~~~~~~

Inside a template definition, template can be used to declare that a dependent name is a template.

songyuanyao
  • 147,421
  • 15
  • 261
  • 354