能否将C++类型模板捕获为模板参数以开发通用容器适配器?
解答
可以实现,你需要的是模板模板参数配合类型萃取的组合方案,你给出的示例代码的模板参数声明存在语法错误,以下是两种符合你需求的实现方式:
方案1:直接传入容器模板作为参数
语法修正后可直接拆分传入容器模板和元素类型:
template< typename T, typename Label = std::string> class foo { // ... }; // 模板参数修正:模板模板参数不需要加<T>后缀,用可变参数兼容STL容器的额外参数(如分配器) template < template <typename, typename...> class Container, typename T, typename Label = std::string > class bar { // 直接用传入的Container模板实例化新元素类型 Container< foo< T, Label > > A; }; // 使用示例 bar<std::vector, int> instance; // 内部成员A的类型为 std::vector<foo<int, std::string>>
方案2:直接传入完整容器类型(如std::vector<int>)
如果你希望像STL适配器一样直接传入实例化后的完整容器类型,只需要额外增加一个容器萃取器即可实现模板重绑定:
// 容器类型萃取器:提取原始模板,支持绑定新元素类型 template <typename Container> struct container_traits; // 偏特化匹配STL风格容器 template <template <typename, typename...> class Container, typename Elem, typename... Args> struct container_traits<Container<Elem, Args...>> { using element_type = Elem; // 绑定新元素类型,保留原容器的其他参数(如分配器) template <typename NewElem> using rebind = Container<NewElem, Args...>; }; template < typename FullContainer, // 直接接受完整容器类型作为参数 typename Label = std::string > class bar { using T = typename container_traits<FullContainer>::element_type; // 重绑定生成新容器类型 using NewContainer = typename container_traits<FullContainer>::template rebind<foo<T, Label>>; NewContainer A; }; // 使用示例,直接传实例化后的容器类型 bar<std::vector<int>> instance; // 内部成员A的类型同样为 std::vector<foo<int, std::string>>
该方案完全兼容所有STL标准容器,不需要硬编码容器类型、分配器等参数,完全符合你开发通用容器适配器的需求,和
std::stack、std::queue等标准适配器的实现思路一致。
内容的提问来源于stack exchange,提问作者joaocandre
相关产品推荐
相关产品推荐

