這是一個包含boost::circular_buffer
約struct
的類。我爲包含circular_buffer
的迭代器製作了一個typedef。std :: upper_bound在const成員函數中返回const迭代器
我的問題是這樣的:當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:答案部分在下面,朋友 –