【发布时间】:2021-10-14 18:12:14
【问题描述】:
是否可以从模板参数中捕获模板,即有一个嵌套的模板说明符和一个包含模板类型的模板参数?
template< typename T, typename Label = std::string>
class foo {
// ...
};
template <
template < typename C, typename T > typename C<T>,
// ...
typename Label = std::string
>
class bar {
// ...
C< foo< T, Label > > A;
};
例如,我想传递一个通用 STL 容器 (std::vector< int >) 作为模板参数,但声明一个具有相同元类型 (std::vector) 但具有不同值类型 (foo< int >) 的成员即std::vector< foo< int > >。这可能看起来很复杂,但最好不要硬编码 STL 容器的类型。
就上下文而言,我的目标是提供一些更高级别的功能的通用容器适配器/包装器(在std::stack 或std::queue 行中)。
【问题讨论】:
标签: c++ templates generics stl containers