这是 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 使用嵌套模板: 我可能弄错了,但我希望编译器为上述方法中的变量类型。
例如,我正在构建一个 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
最佳答案
问题是 stxxl::vector
具有非类型模板参数:
BlockSize
external block size in bytes, default is 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
、...),而不是试图用给定的类型来限制模板参数本身。
关于c++ - Stxxl Vector 作为 std::vector 的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38430146/