0

这是具有通用向量和对类型的向量对的后续,模板的模板。

我希望能够使用std::vectoror调用方法,同时指定 (Pair of x,y) 的stxxl:vector模板参数。vector

具体来说,signatrue 方法可能如下所示:

template<typename t_x, typename t_y,
            template<typename, typename> class t_pair,
            template<typename...> class t_vector>
    method(t_vector<t_pair<t_x,t_y>> &v) 

不幸的是,当像这样指定签名时,不可能传递 a stxxl:vectoras t_vector。它导致以下编译错误:

sad.hpp:128:5: note:   template argument deduction/substitution failed: 
program.cpp:104:52: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class t_vector’
         method(coordinates);
                           ^ 
program.cpp:104:52: error:   expected a type, got ‘4u’ 
program.cpp:104:52: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class t_vector’ 
program.cpp:104:52: error:   expected a type, got ‘2097152u’

问题是如何修改方法签名,以便能够stxxl::vector使用 ? 替代现有代码std::vector

更新为什么我为向量使用嵌套模板:我可能弄错了,但我希望编译器在上述方法中为变量键入类型。

例如,我正在构建一个vector或一个queue

std::vector<t_x> intervals(k * k + 1);
typedef std::tuple<std::pair<t_x,t_y>,std::pair<t_x,t_y>, std::pair<t_x,t_y>, uint> t_queue;
std::queue <t_queue> queue;

哪个应该 uint32_t or uint64_t取决于对元素的类型是uint32_t or uint64_t

4

1 回答 1

3

问题是stxxl::vector具有非类型模板参数:

BlockSize 外部块大小(以字节为单位),默认为 2 MiB

所以它不能匹配反对template <typename... >

在这种情况下,您不应该使用模板模板参数,这样会更好(我认为):

template <typename t_vector>
void method (t_vector &v) {
    typedef typename t_vector::value_type::first_type t_x;
    typedef typename t_vector::value_type::second_type t_y;
    // or in c++11 and above
    typedef decltype(v[0].first) t_xd;
    typedef decltype(v[0].second) t_yd;
}

在上面,您检索t_xt_y使用:

  • value_type这是所有人都Container应该拥有的东西(两者兼有std::vectorstxxl::vector
  • decltype它直接从表达式中获取类型v[0].first(即使v是空的也可以工作,因为里面的表达式decltype永远不会被评估)。

根据我的经验,最好使用一个非常通用的模板参数,然后从中检索信息(value_type, decltype, ...),而不是试图用给定的类型来约束模板参数本身。

于 2016-07-18T06:57:03.233 回答