c++ - Template specialization and inheritance template class from other template class
问题描述
I have some questions about next code:
template<typename T>
class Base;
template<typename T, typename P>
class Base<T(P)> {
};
template<typename T>
class Derived;
template<typename T, typename P>
class Derived<T(P)> : public Base<T(P)> {
};
Class specialization
Derived<T(P)>
inherits from classtemplate<typename T> class Base
or from classtemplate<typename T, typename P> class Base<T(P)>
?What the name of when I'm binding template parameters
T
andP
of class specializationDerived<T(P)>
with template parameterT
ofclass Base
.
解决方案
A class instantiated from the template
Derived<T(P)>
inherits from the class instantiated fromBase<T(P)>
. Since the typeT(P)
matches yourBase
partial specialization, that template will be used to instantiate the class to inherit from.It's just called inheritance. There's nothing special going on there. You're just instantiating the template
Base
with the typeT(P)
and inheriting from the resulting class.
Base<T(P)>
will get instantiated when Derived<T(P)>
gets instantiated, so Derived<void(int)>
inherits from Base<void(int)>
. Both of those instantiations will go through the same set of rules to figure out which template to use to instantiate that class.
You may be getting confused thinking that T(P)
is some special template thing. It is not. It's just the type "function that returns T
and takes a single argument of type P
". Granted, types like that don't come up much outside of templates, but they're perfectly legal other places. i.e.
using FuncType = void(int);
// These two declarations are exactly the same
void doAThing(FuncType* callback);
void doAThing(void(*callback)(int));
推荐阅读
- java - 什么导致网络错误IOException:连接超时:连接?
- angular - 无法为 Ionic4 和 Angular fire 添加 firebase 支持
- powershell - 验证是否制作时无法正常工作?
- java - ClassNotFoundException:用于 SpringBoot 2 功能区启动器的 com.netflix.config.CachedDynamicIntProperty
- c - 我可以强制动态库链接到特定的动态库依赖项吗?
- javascript - 创建中间件全局与本地变量
- html - 将值从 ngFor 内部的元素传递给 Service
- python - Flask-Ask:如何修复“flask.debughelpers.FormDataRoutingRedirect”错误?
- python - 如何将json键转换为小写字母?
- c# - 正则表达式允许 [1-100] 和百分比符号之间的数值