Using declarations for a template function of a template base class

I want to use the base class template function, for example:

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>(); }
};

However, this does not compile.

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 the link to the base class directly, and not to the template, works:

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 the first version does not compile, and the second does. And what do I need to do differently to make it work?

+4
source share
1 answer

The compiler does not know what is fin f<int>()the template, so an error message.

You can do this without usingso:

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>(); }
};
+3
source

Source: https://habr.com/ru/post/1673343/


All Articles