c ++ template:模板容器的分配器 [英] c++ template: allocator for template container

查看:99
本文介绍了c ++ template:模板容器的分配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的c ++模板结构中,我想使用使用不同分配器的不同容器类型,例如stp :: vector和推力::: device_vector。 :

  template< typename T,template< typename,typename>类Container,类型名Alloc> 
struct wrap_into_container
{
typedef Container< T,Alloc>类型;
};

由于不同的容器类使用不同的分配器,因此每次使用时都必须指定相应的分配器此模板。



如何在不指定容器的情况下根据容器类型获取分配器?



我考虑过使用一种特质结构,然后专门针对每种容器类型,但是我不知道如何实现它,或者它是否有用/可能/...



更新:
不幸的是,由于NVIDIA编译器的限制,我无法使用C ++ 11 ...

解决方案

在c ++ 11中,我更喜欢variadics

  template< typename T,template< typename ...> class Container> 
struct wrap_into_container
{
typedef Container< T> :: type type;
};

我没有检查 C :: type 实际上是标准容器类型的格式正确的表达式



评论:

  template< typename T,template< typename ...> class Container> 
struct wrap_into_container
{
typedef Container< T> :: type type;
};

对于C ++ 03,您可以使用嵌套的typedef来模拟模板别名,本质上是使一元类型-该函数采用单个元素类型并返回该类型的容器。概念:

  #include< vector> 
#include< deque>
#include< set>
#include< list>

命名空间容器
{
template< typename T>结构向量{typedef std :: vector< T>类型; };
模板< typename T>结构集{typedef std :: set< T>类型; };
模板< typename T>结构列表{typedef std :: list< T>类型; };
模板< typename T> struct deque {typedef std :: deque< T>类型; };
}

template< typename T,template< typename> class Container>
struct wrap_into_container
{
typedef typename Container< T> :: type type;
};

#include< string>

int main(){

wrap_into_container< int,container :: set> :: type ws;
wrap_into_container< double,container :: list> :: type wl;
wrap_into_container< bool,container :: deque> :: type wd;
wrap_into_container< std :: string,container :: vector> :: type wv;


返回ws.size()+ wl.size()+ wd.size()+ wv.size();

}

查看 在Coliru上直播


In my c++ template struct I want to use different container types which use different allocators, e.g. std::vector and thrust::device_vector.

I need to specify the allocator explicitely, otherwise I get "wrong number of template arguments (1, should be 2)":

template<typename T, template <typename, typename> class Container, typename Alloc>
struct wrap_into_container
{
    typedef Container<T, Alloc> type;
};

Since the different container classes use different allocators, I have to specify the corresponding allocator everytime I want to use this template.

How can I get the allocator depending on the Container type without having to specify it?

I thought of using a traits struct which I then specialize for each Container type, but I don't know how to implement it or if it is even useful / possible / ...

UPDATE: I cannot use C++11 unfortunately due to restrictions of the NVIDIA compiler ...

解决方案

In c++11, I favour variadics

template<typename T, template <typename...> class Container>
struct wrap_into_container
{
    typedef Container<T>::type type;
};

(I haven't checked whether C::type is actually a wellformed expression for standard container types)

To the comment:

template<typename T, template <typename...> class Container>
struct wrap_into_container
{
    typedef Container<T>::type type;
};

For C++03 you can emulate a template alias using nested typedefs, essentially making a unary type-function taking a single element type and returning a container of that type. The concept:

#include <vector>
#include <deque>
#include <set>
#include <list>

namespace container
{
    template <typename T> struct vector { typedef std::vector<T> type; };
    template <typename T> struct set    { typedef std::set   <T> type; };
    template <typename T> struct list   { typedef std::list  <T> type; };
    template <typename T> struct deque  { typedef std::deque <T> type; };
}

template<typename T, template <typename> class Container>
struct wrap_into_container
{
    typedef typename Container<T>::type type;
};

#include <string> 

int main() {

    wrap_into_container<int,         container::set>::type    ws;
    wrap_into_container<double,      container::list>::type   wl;
    wrap_into_container<bool,        container::deque>::type  wd;
    wrap_into_container<std::string, container::vector>::type wv;


    return ws.size() + wl.size() + wd.size() + wv.size();

}

See it Live On Coliru

这篇关于c ++ template:模板容器的分配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆