【发布时间】:2017-01-07 08:58:44
【问题描述】:
这是一个包含一些struct 的boost::circular_buffer 的类。我为包含的circular_buffer 中的迭代器创建了一个typedef。
我的问题是:当doWork函数标记为const时,std::upper_bound的返回值与MyIterator类型不兼容,因为返回值有boost::cb_details::const_traits。如果我从函数中删除 const 关键字,我所有的编译错误都会消失。
要清楚编译器错误是这样的:
error: conversion from ‘boost::cb_details::iterator<boost::circular_buffer<Wrapper<int>::Sample, std::allocator<Wrapper<int>::Sample> >, boost::cb_details::const_traits<std::allocator<Wrapper<int>::Sample> > >’ to non-scalar type ‘Wrapper<int>::MyIterator {aka boost::cb_details::iterator<boost::circular_buffer<Wrapper<int>::Sample, std::allocator<Wrapper<int>::Sample> >, boost::cb_details::nonconst_traits<std::allocator<Wrapper<int>::Sample> > >}’ requested [](const Sample& a, const Sample& b) { return a.foo < b.foo; });
这是一个独立的例子:
#include <algorithm>
#include <boost/circular_buffer.hpp>
template <typename T>
class Wrapper {
public:
struct Sample {
T foo;
};
typedef typename boost::circular_buffer<Sample>::iterator MyIterator;
Wrapper(int size) { cb.resize(size); }
void add(T val) { cb.push_back(Sample{val}); }
void doWork(T bound) const {
MyIterator iter =
std::upper_bound(cb.begin(), cb.end(), Sample{3},
[](const Sample& a, const Sample& b) { return a.foo < b.foo; });
}
boost::circular_buffer<Sample> cb;
};
int main() {
Wrapper<int> buf(100);
buf.add(1);
buf.add(5);
buf.doWork(3);
return 0;
}
那么,为什么这个函数不能是 const 呢?为什么将其标记为 const 会产生这种副作用?我想要一个非常量迭代器进入容器,但在我的真实测试用例中,我根本不打算实际修改容器。
【问题讨论】:
-
既然
doWork是const,cb也被视为const。由于doWork不打算修改cb,请改用const_iterator。 -
一个 MCVE!奇迹永远不会停止。 +1
-
@CaptainObvlious:回答部分在下面,朋友
-
为什么不直接使用
auto iter = std::upper_bound(...? -
@Galik amen to
auto,尤其是因为它被标记为 c++11
标签: c++ c++11 boost circular-buffer