为什么在std :: string` std ::功能上``std :: subrange'''std :: functional` lambda存储在``sTD :: vector'的lambda''中的行为呢?
考虑以下代码,其中包含一个模板std :: vector
std :: function
对象的类型> const& std :: range 和std :: range :: iterator
。导致位置
std :: Ranges :: iterator_t
传递到std :: functional
lambda被无效的原因,以便在上, g ++
和clang ++
,std :: cout
迭代通过std :: string
的结尾?
#include <ranges>
#include <string>
#include <iostream>
#include <vector>
#include <functional>
int main(const int, const char * []) {
auto consume = [](const auto & source, auto position) {
auto consumption = std::ranges::subrange(position, std::end(source));
for (auto res : consumption) {
std::cout << res;
}
};
std::vector<std::function<void(
std::string,
std::string::const_iterator)>> lambdas{consume};
const auto range = std::string("hello");
lambdas.back()(range, std::begin(range));
}
我不确定最小可行的例子 (更好的Asan'd版本由@marek R提供),但我已经尽力修剪了。
Consider the following code, which contains a templated std::vector
type of std::function
objects which consume a const & std::range
and std::range::iterator
. What causes the position
std::ranges::iterator_t
passed into the std::functional
lambda to be invalidated, such that on g++
and clang++
, the std::cout
iteration goes passed the end of the std::string
?
#include <ranges>
#include <string>
#include <iostream>
#include <vector>
#include <functional>
int main(const int, const char * []) {
auto consume = [](const auto & source, auto position) {
auto consumption = std::ranges::subrange(position, std::end(source));
for (auto res : consumption) {
std::cout << res;
}
};
std::vector<std::function<void(
std::string,
std::string::const_iterator)>> lambdas{consume};
const auto range = std::string("hello");
lambdas.back()(range, std::begin(range));
}
I am uncertain what else is unnecessary for the minimal viable example (Better ASAN'd version courtesy of @Marek R), but I've done my best to trim.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论