TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24138 19
Is this UB or a bug in GCC or Clang

Hi, I have run into an issue with capturing coroutines in c++, and I would like to know if it is GCC or Clang that is wrong here. I have a reproducible example(https://godbolt.org/z/9Mh36ro3x), I would expect the code to print "hello" which Clang correctly does, but GCC prints an empty string and I have also seen it print "garbage"(https://godbolt.org/z/a77YsM1fT), and segfault the program. Here is the part of the program that triggers this I think:

auto execute() const {
return & -> boost::asio::awaitable<int> {
mfunction();
co
return 0;
}();
}

I would expect that the captured "this" pointer to be valid until the first yield point since we immediately execute it, and thus the call to m_function should be just fine, which it is in Clang, but this fails catastrophically in GCC.

Which compiler is right, or is it just undefined behavior?

https://redd.it/1ojy2l9
@r_cpp
godbolt.org Compiler Explorer - C++ (x86-64 gcc (trunk)) template<typename F> struct CommandWrapper { F m_function; explicit CommandWrapper(F func) : m_function(std::move(func)) {} auto execute() const { return [&]() -> boost::asio::awaitable<int> { m_function(); co_return…
More from @r_cpp
  1. Oct 14, 2026I tested whether a coding agent would use C++23 ranges without being told Blog post: https…
  2. Oct 14, 2026C++ futures part 1: The mental model https://devblogs.microsoft.com/oldnewthing/20261012-0…
  3. Oct 14, 2026C++ futures part 2: The mental model for std::promise, std::packaged_task https://devblogs…
  4. Oct 12, 2026[Release] slick-logger v2.2.0 — Lock-free asynchronous C++20 logging with multi-process sh…
  5. Oct 12, 2026Cheap alternative to reboot parameters (nohz, rcu, isolcpus) for stable benchmarks When no…
  6. Oct 12, 2026This C++ drama is crazy - Adobe votes no https://www.youtube.com/watch?v=DEqx_NMMH9Q https…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →