【问题标题】:Stxxl Vector as dropin replacement for std::vectorStxxl Vector 作为 std::vector 的 dropin 替代品
【发布时间】:2016-07-18 06:45:46
【问题描述】:

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

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

具体而言,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) 

不幸的是,当这样指定签名时,不可能将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::vector

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

例如,我正在构建 vectorqueue

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

【问题讨论】:

  • 为什么需要这样指定模板参数?为什么不使用没有其他所有参数的简单typename t_vector?如果您要使用此模板参数来构造各种类型,则使用模板模板参数很有用,但在您的情况下,您只能将它与一种类型一起使用,即t_pair
  • 我使用 uint32_t 和 uint64_t 作为 pair 的模板参数。因此,我在代码中使用 t_x/t_y 来决定是否应该使用 32/64 位整数。
  • 我真的不明白为什么按照简单表单的方式使用模板会对您有所帮助?您能否更新您的问题,向我们确切说明为什么需要这样的模板参数(以及为什么更简单的形式不适合您)?
  • @Holt 感谢您到目前为止的回答,我希望通过更新问题澄清我的意图
  • 感谢您的更新,请参阅我的更新答案,了解(我认为更好)做您想做的事情的方式。

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


【解决方案1】:

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

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

所以它不能与template &lt;typename... &gt;匹配。

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

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、...),而不是尝试使用给定类型约束模板参数本身。

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 2014-01-11
    • 1970-01-01
    • 2023-03-25
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2023-03-23
    相关资源
    最近更新 更多