Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using declarations for template function of template base class

I want to use a template function of a base class, like this:

struct A {
  template <class T> static auto f() { \*code*\ }
};

template <class A_type> struct B : public A_type {

  using A_type::f;

  void g() { auto n = f<int>(); }
};

This does not compile however.

error: expected '(' for function-style cast or type
      construction
  void g() { auto n = f<int>(); }
                        ~~~^
error: expected expression
  void g() { auto n = f<int>(); }

But referencing the base class directly instead of the template does work:

struct A {
  template <class T> static auto f() { \*code*\ }
};

template <class A_type> struct B : public A_type {

  using A::f;

  void g() { auto n = f<int>(); }
};

Why does the first version not compile and the second does. And what would I need to do differently to make it work?

like image 318
lo tolmencre Avatar asked Oct 17 '22 15:10

lo tolmencre


1 Answers

The compiler doesn't know that the f in f<int>() is a template, hence the error message.

You can do it without the using like so:

struct A {
    template <class T> static auto f() { /*code*/ }
};

template <class A_type> struct B : public A_type {
    void g() { auto n = A_type::template f<int>(); }
};
like image 98
emlai Avatar answered Oct 21 '22 05:10

emlai