boost::adaptor::filters core dumps with boost::range_detail::d efault_constructible_unary_fn_wrapper
boost::adaptor::filtered core dumps with boost::range_detail::default_constructible_unary_fn_wrapper "Assertion `m_impl' failed"
运行此代码时,boost::range_detail::default_constructible_unary_fn_wrapper
内部出现断言失败。断言似乎在检查函子是否已在筛选器对象内部初始化。
#include <boost/range/adaptor/filtered.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
template<class RangeType>
static auto get_range_and_range_size(const RangeType& in_range, std::input_iterator_tag) -> auto {
/*
* we have an InputRange, but we need to know the size of the range.
* so we eagerly copy the values into a vector so we can get the size of the range
* and also return the range.
* */
auto out_range = std::vector<decltype(*std::begin(in_range))>(
std::begin(in_range),
std::end(in_range)
);
return std::make_tuple(std::move(out_range), out_range.size());
}
template<class RangeType>
static auto get_range_and_range_size(const RangeType& in_range, std::forward_iterator_tag) -> auto {
return std::make_tuple(std::ref(in_range), boost::distance(in_range));
}
struct Cache {
std::unordered_map<int, int> m_map;
template<class RangeT>
auto Insert(RangeT in_range) -> void {
typename std::iterator_traits<
decltype(std::begin(in_range))
>::iterator_category iterator_category;
std::cout << "No core dump yet!n";
auto range_and_range_size = get_range_and_range_size(
boost::adaptors::filter(
in_range,
/*
* filter out any keys that are already in the cache and NOT TTLed yet
* */
[this](const auto& key_value) -> bool {
const auto find_itr = m_map.find(key_value.first);
return (m_map.end() == find_itr) /* the key was not in the cache */
|| hasTTLed(find_itr->second); /* the key was in the cache but its value has TTLed */
}
),
iterator_category
);
for(auto&& key_value : std::get<0>(range_and_range_size)) {
m_map.emplace(key_value);
}
std::cout << "Can't reach this code. :(n";
}
auto hasTTLed(int) const noexcept -> bool {
/*
* TTL impl goes here
*/
return false;
}
};
auto main(int, char*[]) -> int {
Cache a{};
std::vector<std::pair<int, int>> v{
{0, 0},
{1, 1},
{2, 2},
{3, 3}
};
a.Insert(v);
std::map<int, int> b{
{0, 1},
{1, 0},
{2, 1},
{3, 1}
};
a.Insert(b);
}
无论coliru使用什么版本的boost,以及1.60,我都能得到这个。
在coliru上复制。
你能帮我了解a)这里发生了什么吗?b)我是如何解决的?
这是一个生存期问题。
问题出现在get_range_and_range_size
中,它正在捕获const RangeType&
,而本应捕获RangeType&&
。将std::ref
返回到以这种方式捕获的临时范围会导致调用作用于range_and_range_size
的代码中未初始化的范围。
get_range_and_range_size
的这些定义解决了这个问题。
template<class RangeType>
static auto get_range_and_range_size(RangeType&& in_range, std::input_iterator_tag) -> auto {
/*
* we have an InputRange, but we need to know the size of the range.
* so we eagerly copy the values into a vector so we can get the size of the range
* and also return the range.
* */
auto out_range = std::vector<decltype(*std::begin(in_range))>(
std::begin(in_range),
std::end(in_range)
);
const auto out_size = out_range.size(); /* capture the value before moving the container */
return std::make_tuple(std::move(out_range), out_size);
}
template<class RangeType>
static auto get_range_and_range_size(RangeType&& in_range, std::forward_iterator_tag) -> auto {
const auto size = boost::distance(in_range); /* capture the value before moving the container */
return std::make_tuple(std::move(in_range), size);
}
相关文章:
- 理解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 Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- Boost::posix_time::ptime舍入到给定的分钟数
- boost xml parsingl将xml的路径作为变量发送