首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Stxxl载体替代std::载体

Stxxl载体替代std::载体
EN

Stack Overflow用户
提问于 2016-07-18 06:45:46
回答 1查看 303关注 0票数 0

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

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

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

代码语言:javascript
复制
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。这将导致以下编译错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-18 06:57:03

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

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

因此,它无法与template <typename... >相匹配。

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

代码语言:javascript
复制
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,.)而不是试图用给定的类型约束模板参数本身。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38430146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档