这是Vector of pairs with generic vector and pair type, template of template的后续内容。
我希望能够使用month
或std::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::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
答案 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_x
和t_y
value_type
这是所有Container
应该拥有的内容(std::vector
和stxxl::vector
都有); decltype
直接从表达式v[0].first
中获取类型(即使v
为空也有效,因为永远不会评估decltype
中的表达式)。根据我的经验,最好使用一个非常通用的模板参数,然后从中检索信息(value_type
,decltype
,...),而不是试图用给定的约束模板参数本身类型。