来自包扩展的多重继承
我最近在生产代码中看到了这一点,但不太明白它的作用:
template <class... Ts>
struct pool : pool_type<Ts>... {
//...
};
我从未见过父类发生包扩展。它只是继承传递给可变参数的每种类型吗?
父母的表情是这样的:
template <class T>
struct pool_type : pool_type_impl<T> {
// ...
};
I recently saw this in production code and couldn't quite figure out what it does:
template <class... Ts>
struct pool : pool_type<Ts>... {
//...
};
I've never seen pack expansion happening for parent classes. Does it just inherit every type passed into the varargs?
The parents look like this:
template <class T>
struct pool_type : pool_type_impl<T> {
// ...
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。它公开继承每个传递的参数。下面给出了一个简化版本。
来自参数包的文档:
示例
上述程序的输出可以在此处查看:
解释
在上面的代码中,
X< /code> 类模板使用包扩展来获取每个提供的 mixin 并将其扩展为公共基类。换句话说,我们得到了
X
公开继承的基类列表。此外,我们还有一个 X 构造函数,它从提供的构造函数参数中复制初始化每个 mixin。Yes. It inherits publicly from each of the passed arguments. A simplified version is given below.
From Parameter pack's documentation:
Example
The output of the above program can be seen here:
Explanation
In the above code, the
X
class template uses a pack expansion to take each of the supplied mixins and expand it into a public base class. In other words, we get a list of base classes from whichX
inherits publicly. Moreover, we also have aX
constructor that copy-initializes each of the mixins from supplied constructor arguments.