C++ 使用模板基类的模板函数声明
我想使用基类的模板函数,如下所示:C++ 使用模板基类的模板函数声明,c++,templates,inheritance,using-statement,C++,Templates,Inheritance,Using Statement,我想使用基类的模板函数,如下所示: 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>(); } }; 结构A{ 模板静态自动f(){\*代码*\} }; 模板结构B:公共A_类型{
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>(); }
};
结构A{
模板静态自动f(){\*代码*\}
};
模板结构B:公共A_类型{
使用A_类型::f;
void g(){auto n=f();}
};
然而,这并不编译
error: expected '(' for function-style cast or type
construction
void g() { auto n = f<int>(); }
~~~^
error: expected expression
void g() { auto n = f<int>(); }
错误:函数样式转换或类型应为“(”
建设
void g(){auto n=f();}
~~~^
错误:应为表达式
void g(){auto n=f();}
但是直接引用基类而不是模板确实有效:
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>(); }
};
结构A{
模板静态自动f(){\*代码*\}
};
模板结构B:公共A_类型{
使用A::f;
void g(){auto n=f();}
};
为什么第一个版本不编译,第二个版本编译。我需要做什么不同的事情才能使它工作?编译器不知道
f()
中的f
是一个模板,因此出现了错误消息
您可以在不使用的情况下使用执行此操作,如下所示:
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>(); }
};
结构A{
模板静态自动f(){/*代码*/}
};
模板结构B:公共A_类型{
void g(){auto n=A_type::template f();}
};
而且没有办法明确地告诉它它是一个模板?在进行实验时,我从编译器那里得到了一些信息,关于在using语句中的某个地方添加“模板”,但我不知道ad在哪里无法复制该提示。我没有找到一种方法。我尝试在所有可能的地方添加模板,但根本问题似乎是你不能用
声明来模板化,也没有函数别名。好的,谢谢。我记得我是怎么被击中的。击中的是错误:使用'template'关键字将'f'作为一个依赖的模板名用a_type::f;
我是通过使用a_type::f;
而不是使用a_type::f;