【发布时间】:2017-12-04 07:32:15
【问题描述】:
给定以下函数签名
void foo(std::vector<int> &bar);
还有一个自定义分配器CustomAlloc,使用std::vector<int, CustomAlloc> 的实例调用foo 会导致
could not convert ‘bar’ from ‘std::vector<int, CustomAlloc<int, [...] >}’ to ‘std::vector<int>’
我不是 C++ 模板专家,如果我错了,请纠正我,但我的理解是 std::vector<int> 默认为 std::vector<int, std::allocator<int> > (gcc 7.1.1.-3),因此定义了一个完全不相关的类型。
这个beeing说我正在尝试找到一些解决方案来使foo可以使用std::vector<int, CustomAlloc>调用并附带以下内容。
template<typename Allocator>
void foo(std::vector<int, Allocator> &bar);
我不喜欢分配器规范传播模板的方式,但我猜这就是 STL 的工作方式。
所以我的问题是,鉴于foo 专门在int 上运行,您将如何解决这个问题?
PS:抱歉标题不好,我只是找不到好标题
【问题讨论】:
-
不清楚是否需要
foo接受不同类型的参数,但如果要求只使用CustomAlloc,则可以不带任何模板声明void foo(std::vector<int, CustomAlloc> &bar);。
标签: c++ c++11 templates vector allocator