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?
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>(); }
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With