这是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。
发布于 2016-07-18 06:57:03
问题是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,.)而不是试图用给定的类型约束模板参数本身。
https://stackoverflow.com/questions/38430146
复制相似问题