[英]range-v3: strange Behavior
I am trying to play with range-v3 and I encountered a problems : it does not extract values from a vector as I would have wanted. 我正在尝试使用range-v3,但遇到了一个问题:它没有像我想要的那样从向量中提取值。
See the code below: 请参见下面的代码:
When running, it outputs (0, 0), instead of what I would have thought, ie (1, 0) 在运行时,它输出(0,0),而不是我所想的,即(1,0)
If I uncomment the line auto pairs = ...
, then the result is changed, and the output becomes (33144464,0), although the variable pairs
is unused (and the assert will fail) 如果我取消注释行auto pairs = ...
,则结果将更改,并且输出变为(33144464,0),尽管变量pairs
未使用(并且断言将失败)
#include <iostream>
#include <vector>
#include <range/v3/all.hpp>
auto foo()
{
auto values = std::vector<int> { 1, 0 };
// auto pairs = std::vector< std::pair<int, int> > { { 1, 0 }, { 0, 1 }, { 0, 0 } };
return ranges::view::for_each(values, [=](int nb)
{
std::cout << " nb=" << nb << std::endl;
assert( (nb == 0) || (nb == 1) );
return ranges::yield(nb);
});
}
int main()
{
ranges::for_each(foo(), [](auto v) {
std::cout << v << "\n";
});
}
This code was compiled with g++ (g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0) and clang++ (clang version 8.0.0 (tags/RELEASE_800/final)) with the following commands: 该代码是使用以下命令使用g ++(g ++(Ubuntu 7.3.0-27ubuntu1〜18.04)7.3.0)和clang ++(clang版本8.0.0(tags / RELEASE_800 / final))编译的:
g++ foo.cpp -std=c++14 -Irange-v3/include -Wall -Wpedantic
clang++ foo.cpp -std=c++14 -Irange-v3/include -Wall -Wpedantic
I am using a fresh clone for ranges-v3, and I can reproduce this on ubuntu and OSX (with AppleClang). 我正在使用Range-v3的新克隆,并且可以在ubuntu和OSX(使用AppleClang)上复制它。
There is a experimental feature coming in clang called -Wlifetime
that can be used on godbolt . -Wlifetime
中有一个实验功能,称为-Wlifetime
,可以在godbolt上使用 。 It gives the following warnings pointing to the return from foo
. 它给出以下警告,指出从foo
返回。
[x86-64 clang (experimental -Wlifetime) #1] warning: returning a dangling Pointer [-Wlifetime]
[x86-64 clang (experimental -Wlifetime) #1] note: pointee 'values' left the scope here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.