Stxxl Vector作为std :: vector的dropin替换

时间:2016-07-18 06:45:47

标签: c++ c++11 vector stl stxxl

这是Vector of pairs with generic vector and pair type, template of template的后续内容。

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

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

vector

不幸的是,在指定此类签名时,无法将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) 作为stxxl:vector传递。它导致以下编译错误:

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::vectorvector

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

1 个答案:

答案 0 :(得分: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_typedecltype,...),而不是试图用给定的约束模板参数本身类型。