【发布时间】:2016-07-18 06:45:46
【问题描述】:
这是Vector of pairs with generic vector and pair type, template of template的后续。
我希望能够使用std::vector 或stxxl: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?
关于为什么我为向量使用嵌套模板的更新: 我可能弄错了,但我希望编译器为上述方法中的变量键入类型。
例如,我正在构建 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
【问题讨论】:
-
为什么需要这样指定模板参数?为什么不使用没有其他所有参数的简单
typename t_vector?如果您要使用此模板参数来构造各种类型,则使用模板模板参数很有用,但在您的情况下,您只能将它与一种类型一起使用,即t_pair。 -
我使用 uint32_t 和 uint64_t 作为 pair 的模板参数。因此,我在代码中使用 t_x/t_y 来决定是否应该使用 32/64 位整数。
-
我真的不明白为什么按照简单表单的方式使用模板会对您有所帮助?您能否更新您的问题,向我们确切说明为什么需要这样的模板参数(以及为什么更简单的形式不适合您)?
-
@Holt 感谢您到目前为止的回答,我希望通过更新问题澄清我的意图
-
感谢您的更新,请参阅我的更新答案,了解(我认为更好)做您想做的事情的方式。
标签: c++ c++11 vector stl stxxl