Stay up-to-date with everything C++!
Content directly fetched from the subreddit just for you.
Join our group for discussions : @programminginc
Powered by : @r_channels
Post #23941
13
A bug with std::generator move assignment?
I was playing with `std::generator` and planning to incorporate into our project. One thing I came across is that the move assignment of `std::generator` seems not working.
The code is as follows. I have a `Request` object which is intended to be reused repeatedly. `Request` has a memeber variable `coro_` of `std::generator`, which captures the computation logic of the request. Since `Request` is recycled and reused, `std::generator` needs to be reset. When using the move assignment to reset `coro_ = Generate(num);`, the program always crashes with varying error codes.
`[1] 3545 illegal hardware instruction ./generator`
`[1] 3561 illegal hardware instruction ./generator`
`[1] 3571 illegal hardware instruction ./generator`
#include <generator>
#include <iostream>
std::generator<bool> Generate(int num)
{
if (num < 0)
{
num = 0;
}
for (int i = 0; i < num; ++i)
{
co_yield false;
}
co_yield true;
}
class Request
{
public:
Request()
: coro_([]() -> std::generator<bool> { co_yield true; }()),
iter_(coro_.begin()),
has_stack_(true)
{
}
~Request() = default;
bool Resume()
{
bool ret = *iter_;
if (iter_ != coro_.end())
{
++iter_;
}
return ret;
}
void Reset(int num)
{
if (has_stack_)
{
coro_.~generator();
}
coro_ = Generate(num);
iter_ = coro_.begin();
// Placement new
// auto *tmp = new (&coro_) std::generator<bool>(Generate(num));
// iter_ = tmp->begin();
has_stack_ = true;
}
private:
std::generator<bool> coro_;
std::ranges::iterator_t<decltype(coro_)> iter_;
bool has_stack_{false};
};
int main()
{
Request req;
req.Reset(6);
size_t i = 0;
while (!req.Resume())
{
std::cout << "Process the req, iteration #" << i << std::endl;
i++;
}
std::cout << "Req is done!" << std::endl;
return 0;
}
The environment is WSL2. The kernel is 6.6.87.2-microsoft-standard-WSL2. The compiler is GCC 14.2.0. The interesting part is that I can circumvent the issue by using placement new (the commented code):
`new (&coro_) std::generator<bool>(Generate(num));`
I am wondering if anyone ever encountered a similar issue? Is this a bug with current GCC14?
https://redd.it/1nu7w4g
@r_cpp
I was playing with `std::generator` and planning to incorporate into our project. One thing I came across is that the move assignment of `std::generator` seems not working.
The code is as follows. I have a `Request` object which is intended to be reused repeatedly. `Request` has a memeber variable `coro_` of `std::generator`, which captures the computation logic of the request. Since `Request` is recycled and reused, `std::generator` needs to be reset. When using the move assignment to reset `coro_ = Generate(num);`, the program always crashes with varying error codes.
`[1] 3545 illegal hardware instruction ./generator`
`[1] 3561 illegal hardware instruction ./generator`
`[1] 3571 illegal hardware instruction ./generator`
#include <generator>
#include <iostream>
std::generator<bool> Generate(int num)
{
if (num < 0)
{
num = 0;
}
for (int i = 0; i < num; ++i)
{
co_yield false;
}
co_yield true;
}
class Request
{
public:
Request()
: coro_([]() -> std::generator<bool> { co_yield true; }()),
iter_(coro_.begin()),
has_stack_(true)
{
}
~Request() = default;
bool Resume()
{
bool ret = *iter_;
if (iter_ != coro_.end())
{
++iter_;
}
return ret;
}
void Reset(int num)
{
if (has_stack_)
{
coro_.~generator();
}
coro_ = Generate(num);
iter_ = coro_.begin();
// Placement new
// auto *tmp = new (&coro_) std::generator<bool>(Generate(num));
// iter_ = tmp->begin();
has_stack_ = true;
}
private:
std::generator<bool> coro_;
std::ranges::iterator_t<decltype(coro_)> iter_;
bool has_stack_{false};
};
int main()
{
Request req;
req.Reset(6);
size_t i = 0;
while (!req.Resume())
{
std::cout << "Process the req, iteration #" << i << std::endl;
i++;
}
std::cout << "Req is done!" << std::endl;
return 0;
}
The environment is WSL2. The kernel is 6.6.87.2-microsoft-standard-WSL2. The compiler is GCC 14.2.0. The interesting part is that I can circumvent the issue by using placement new (the commented code):
`new (&coro_) std::generator<bool>(Generate(num));`
I am wondering if anyone ever encountered a similar issue? Is this a bug with current GCC14?
https://redd.it/1nu7w4g
@r_cpp