如何创建const boost::iterator_range
How do I create a const boost::iterator_range
为什么boost::find_first对其输入进行非常量引用?建议"调用者使用const_iterator模板参数创建一个非常量迭代器_range,以"证明"迭代对象有足够的生存期。"
这意味着什么?我该怎么做?
特别是,如何使用此代码实现常量正确性?
typedef std::map<int, double> tMyMap;
tMyMap::const_iterator subrange_begin = my_map.lower_bound(123);
tMyMap::const_iterator subrange_end = my_map.upper_bound(456);
// I'd like to return a subrange that can't modify my_map
// but this vomits template errors complaining about const_iterators
return boost::iterator_range<tMyMap::const_iterator>(subrange_begin, subrange_end);
对范围进行非常量引用可避免绑定到临时性
我会让编译器做你的工作来避免你的难题²:
tMyMap const& my_map; // NOTE const
// ...
return boost::make_iterator_range(my_map.lower_bound(123), mymap.upper_bound(456));
标准C++延长了绑定到常量引用变量的临时变量的生存期,但这不适用于绑定到对象成员的引用。因此,通过引用聚合范围很容易出现这种错误。
/OT:IMO甚至和的预防措施/检查一些Boost Range功能(如适配器)通常太不安全,无法使用;我陷入这些陷阱的次数比我愿意承认的要多。
²除了我们无法从您提供的样本中复制之外
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- 造成致命错误:boost/fusion/iterator/equal_to.hpp 没有这样的文件或目录
- 如何从 boost::container::vector<std::string>::iterator 访问索引和对象?
- boost::asio::ip::tcp::resolver::iterator 检查值是否为空
- 来自async_resolve的 boost::asio::ip::tcp::resolver::iterator 的生命周期是多久?
- Clang Modules 与 std <iterator> 和 <boost/move/iterator.hpp 的交互>
- 为什么箭头运算符"->"不适用于 boost::numeric::ublas::vector<...>::iterator?
- 标记字符串并将结果存储在 boost::iterator_range<std::string::iterator 中>
- 将 boost::p tr_vector<char>::iterator itr 转换为其中的数据?
- 如何转发声明 boost::p tree::iterator