C ++从模板参数创建元组的向量

时间:2018-06-13 10:35:31

标签: c++ templates boost tuples

我有以下代码:

template <typename Type, typename Handler>
struct InternalColumn
{
   column(Type* data, Handler h): data_(data), h_(h) {}

   // overloaded functions for data binding ( int, char, long..)
   template<typename SourceData>
   void bindData(SourceData src){...}
...

Type* data_; // pointer to external data
Handler h_; // for some reasons
};

template <typename Columns, typename Handler>
writeDataFunc(Columns&& cols, Handler h)
{
   // here i need to create here some tuples InternalColumn<T,H> vector
   // vector<InternalColumn<T,H>> vt;   how can I create it from incoming vector?
   // then in loop - internCol.bind(col);
   // 
   // write data 
   //
   // call handler h(cols); 
}

// i also have stuff like this:
template<typename T, typename H>
auto makeColumn(T* data, H h) 
{
   return column<T, H>(data, h);
}

main()
{
   int i= 0;
   char c[32] = {0};
   long l = 0;

   writeDataFunc(boost::fusion::vector_tie(i,c,l), [&](auto& col)
   {
     // working with i, c, l variables
   });
}

所以我的问题出在writeDataFunc我无法在外面创建 InternalColumns并将其传递给vector_tie。 它应该是POD类型。

我的问题是: 是否可以在writeDataFunc中创建一个InternalColumn向量(boost,元组向量)?

请有任何想法或者我不清楚。

1 个答案:

答案 0 :(得分:1)

除非它们具有相同的类型T,否则不能创建InternalColumn的std :: vector,所以我想你想创建融合向量。我使用了std :: tuple,这非常相似。只要InternalColumn类型不同,下面的解决方案就可以工作(因为我使用类型作为std :: get的键)。为了概括它,可以创建一个std :: index_sequence并通过索引

访问元组
#include <tuple>                                                                                                                                                                                                                                                                          
template <typename Type, typename Handler>                                                                                                                                                                                                                                                
struct InternalColumn                                                                                                                                                                                                                                                                     
{                                                                                                                                                                                                                                                                                         
   InternalColumn(Type& data, Handler h): data_(data), h_(h) {}                                                                                                                                                                                                                           

   // overloaded functions for data binding ( int, char, long..)                                                                                                                                                                                                                          
   template<typename SourceData>                                                                                                                                                                                                                                                          
   void bindData(SourceData src){}                                                                                                                                                                                                                                                        


Type& data_; // pointer to external data                                                                                                                                                                                                                                                  
Handler h_; // for some reasons                                                                                                                                                                                                                                                           
};                                                                                                                                                                                                                                                                                        

template<typename Lambda, typename Tuple, int ID=0>                                                                                                                                                                                                                                       
void for_each_tuple(Lambda&& l, Tuple& t){                                                                                                                                                                                                                                                
    l(std::get<ID>(t));                                                                                                                                                                                                                                                                   
    if constexpr(ID < std::tuple_size<Tuple>::value-1)                                                                                                                                                                                                                                    
                    for_each_tuple<Lambda, Tuple, ID+1>(l, t);                                                                                                                                                                                                                            
}                                                                                                                                                                                                                                                                                         

template <template <typename ...> class Tuple, typename Handler, typename ... Types>                                                                                                                                                                                                      
int writeDataFunc(Tuple<Types ...>&& cols, Handler h)                                                                                                                                                                                                                                     
{                                                                                                                                                                                                                                                                                         

    Tuple<InternalColumn<Types, Handler> ...> vt{InternalColumn{std::get<Types>(cols), h} ...};                                                                                                                                                                                           

    for_each_tuple(h, vt);                                                                                                                                                                                                                                                                
}                                                                                                                                                                                                                                                                                         

// i also have stuff like this:                                                                                                                                                                                                                                                           
template<typename T, typename H>                                                                                                                                                                                                                                                          
auto makeColumn(T* data, H h)                                                                                                                                                                                                                                                             
{                                                                                                                                                                                                                                                                                         
   return InternalColumn<T, H>(data, h);                                                                                                                                                                                                                                                  
}                                                                                                                                                                                                                                                                                         

main()                                                                                                                                                                                                                                                                                    
{                                                                                                                                                                                                                                                                                         
   int i= 0;                                                                                                                                                                                                                                                                              
   char c[32] = {0};                                                                                                                                                                                                                                                                      
   long l = 0;                                                                                                                                                                                                                                                                            

   writeDataFunc(std::make_tuple(i,c,l), [&](auto& col)                                                                                                                                                                                                                                   
   {                                                                                                                                                                                                                                                                                      
     // working with i, c, l variables                                                                                                                                                                                                                                                    
   });                                                                                                                                                                                                                                                                                    
}