3

I am using the CRTP and the base class has a template function. How can I use that member function in a templated derived class?

template <typename T>
struct A {
  int f();
  template <typename S>
  int g();
};
struct B: public A<B> {
  int h() { return f() + g<void>(); } // ok
};
template <typename T>
struct C: public A<C<T>> {
  // must 'use' to get without qualifying with this->
  using A<C<T>>::f; // ok
  using A<C<T>>::g; // nope
  int h() { return f() + g<void>(); } // doesn't work
};

* Edit * An earlier question, Using declaration for type-dependent template name, including the comments, suggests this isn't possible and might be an oversight in the standard.

Community
  • 1
  • 1
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110

1 Answers1

2

I don't know how to solve the problem with using statement (it should look something like using A<C<T>>::template g;, but this code does not compile with my compiler). But you may call g<void> method in one of the following ways:

  • this->template g<void>()

  • A<C<T>>::template g<void>()

See the answers to this question for details about the dark side of using template keyword.

Community
  • 1
  • 1
Constructor
  • 7,273
  • 2
  • 24
  • 66
  • Yes, your other two statements are how I would access g without using `using`, but they're both pretty cumbersome! I was hoping a magic incantation of `template` let me avoid them. – pythonic metaphor Feb 26 '14 at 23:04