【发布时间】:2017-09-02 21:49:25
【问题描述】:
我有一个框架,它需要一个迭代器作为输入。 但有时,我只有一个元素,所以构建容器似乎工作量太大。
T obj; // my unique object
std::vector<T> vec; // I want to avoid this
vec.push_back(T);
// Because the only use of the container is for this call
call(std::begin(vec), std::end(vec));
// I want to do something like that
call(BeginFakeSingletonIt<T>(obj), EndFakeSingletonIt<T>());
我可以创建一种特殊类型的迭代器,但标准库或 boost 中不存在类似的东西吗?
【问题讨论】:
-
快速破解:
call(&obj, &obj + 1); -
@nwp:不是黑客,完全合法。
-
如果您可以将代码重写为
T obj[1];,那么您也可以使用std::begin(obj), std::end(obj)。