模板类上的类型继承
Inheritance of type on a template class
我正在尝试在子类中的新函数定义中使用父模板类中的类型,但我无法对其进行编译。
但是,如果未定义myecho,它确实会编译和执行(子类中未使用回调(
我已经尝试过:
-
无定义int myecho(T arg, callback cbk(
-
使用范围int myecho(T arg, Foo::callback cbk(int myecho(T arg, Foo::callback cbk(
-
使用辛税使用 Foo::回调;
#include <cstdio>
#include <iostream>
#include <functional>
template <class T>
class Foo
{
public:
using callback = std::function<int (T param)>;
Foo() = default;
virtual ~Foo() = default;
int echo(T arg, callback cbk) { return cbk(arg);}
};
template <class T>
class _FooIntImp : public Foo<T>
{
public:
using Foo<T>::echo;
_FooIntImp() = default;
virtual ~_FooIntImp() = default;
int myecho(T arg, callback cbk)
{
return 8;
}
};
using FooInt = _FooIntImp<int>;
int mycallback( int param )
{
return param * param;
}
int main(int argc, char* argv[] )
{
FooInt l_foo;
std::cout << "Out "<<l_foo.echo(43,mycallback) << std::endl;
return 0;
}
你可以把它写成
int myecho(T arg, typename Foo<T>::callback cbk)
// ^^^^^^^^^^^^^^^^^
{
return 8;
}
或通过using
介绍名称。
using typename Foo<T>::callback;
int myecho(T arg, callback cbk)
{
return 8;
}
相关文章:
- 继承模板类中的类型别名
- 在运行时检查继承是否只有一种类型和 void*
- C++从外部类继承的嵌套类;不允许使用不完整的类型
- 可以从std::string继承以提供类型一致性吗
- 事件系统:使用类型转换或联合进行继承
- 继承类中的 C++ 成员变量类型重写
- C :可以从类及其受保护的成员类型继承可以继承吗?
- 模板类上的类型继承
- C 类从类型继承
- 在 C++11 中,从私有嵌套类型继承是否合法?
- 模板基类型继承
- C#中从指定类型继承的模板类的等效项是什么
- 为boost mpl列表中的每种类型继承容器
- 从基类和返回类型继承模板
- 从基元类型继承的枚举
- 使用类型继承的数字组的层次结构
- 在任何/某些C++编译器中是否有允许从本机(基本)类型继承的选项
- 创建从不同对象提供的类型继承的类的对象
- c++用这个类作为类型继承类模板
- 我如何从mpl::vector中的所有类型继承