如何编写作用在嵌套STL容器上并限制最内在类型的模板功能的模板功能
how to write a template of template function acting on nested stl containers and restricting the innermost type
我当前正在使用STL容器模板的模板
进行实验template <template <typename, typename > class Container, typename element, typename Allocator>
void printsize (Container<element, Allocator> & a){
std::cout<<"container size: "<<a.size()<<std::endl;
};
int main(){
std::vector<double> testvector;
for(int i = 0; i < 4; ++i){
testvector.push_back((double) i);
}
printsize(testvector);
std::list<std::vector<double> > testlist;
for(int i = 0; i < 8; ++i){
testlist.push_back(testvector);
}
printsize(testlist);
}
正确输出为
container size: 4
container size: 8
我试图限制模板类型的范围,该功能接受为
之类的东西stl::container<double>.
如果我尝试这样的事情:
template <template <typename, typename > class Container, typename Allocator>
void printsize (Container<double , Allocator> & a){
std::cout<<"container size: "<<a.size()<<std::endl;
};
将使编译器抱怨
std::vector<int>
按预期的容器,但是它不再在
上起作用std::list<std::vector< double> >
因为,double是与std :: vector
不同的类型但是,我希望我的功能在A 特定的"基础"类型的任意STL容器和STL容器(等)上作用。。
背后的原因是我不想使用很多过载。基本上我要实现的是编写一些功能
B dosomestuff(A a){
B b = someWorkDoneOnA(a);
return b;
}
stl::container<A> dosomestuff(stl::container<B> myContainer){
B newContainerElement;
stl::container<A> outputContainer;
for(auto i& : myContainer){
newContainerElement = dosomestuff(i);
outputContainer.push_back(newContainerElement);
}
return outputContainer;
};
到目前老实说,就这样,我认为我不需要更多,但是谁知道?)。实际上,它是同一件代码(类型声明除外),即湿法。
让我们从" b的n-nested容器"的类型特征开始(其中n == 0是b)
template <typename T>
struct is_B : std::false_type;
template <template <typename, typename > class Container, typename Element, typename Allocator>
struct is_B<Container<Element, Allocator>> : std::is_B<Element>;
template <>
struct is_B<B> : std::true_type;
然后,您可以使用enable_if增强工作模板:
template <template <typename, typename > class Container, typename Element, typename Allocator>
void printThings(Container<std::enable_if<is_B<Element>::value, Element>, Allocator>) ...
这使我们一半。然后,我们制作收集类型的构造函数模板
template <typename T>
struct Bs_to_As {};
template <template <typename, typename > class Container, typename Element, typename Allocator>
struct Bs_to_As<Container<Element, Allocator>> { typedef Container<Bs_to_As<Element>::type, Allocator> type; }
template <>
struct Bs_to_As<B> { typedef A type; }
template <template <typename, typename > class Container, typename Element, typename Allocator>
Container<Bs_to_As<Element>, Allocator> doStuff(Container<Element, Allocator> myContainer)
{ ... }
相关文章:
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- C++从成员类型中扣除类型的功能模板?
- 这种错误的原因是什么:将"功能"重新定义为不同类型的符号
- 模板功能:根据类型名称进行转换
- 如何在同一个模板功能上专门化几种类型?
- 错误:未知类型名称"功能跟踪"
- 我对C 中共享指针列表进行排序的功能未完成类型
- 共享_ptr中多功能数组的类型是什么
- 确定功能主体中的返回类型
- 为什么BOOST :: HANA :: EXAREMIS ::键入实验功能?类型列表的棘手是什么
- rvalue参考绑定到std ::功能类型的LVALUE
- 我可以创建课堂功能类型吗?
- 获取模板功能函数返回类型的功能类型
- 非分解功能类型可以是非类型参数
- 想要实现将功能类型作为参数的信号模板之类的启动
- 为什么类型扣除会失败(非销售)功能类型
- 限制(AMP)功能类型
- 使用 C++14 的自动功能类型返回扣除代替 std::common_type 是否始终安全?
- 从OpenCV FeatureDetector检索功能类型