Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does 'using' directive work with template member functions

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.

like image 476
pythonic metaphor Avatar asked Jun 06 '26 02:06

pythonic metaphor


1 Answers

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.

like image 131
Constructor Avatar answered Jun 07 '26 17:06

Constructor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!