如何编写作用在嵌套STL容器上并限制最内在类型的模板功能的模板功能

how to write a template of template function acting on nested stl containers and restricting the innermost type

本文关键字:功能 类型 嵌套 作用 何编写 STL      更新时间:2023-10-16

我当前正在使用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)
{ ... }