Date: Wed, 18 Dec 2024 19:59:18 +0800 (CST)
Hi all,
The following code gives my expected result using MSVC, but a unpredictable result using GCC and clang. Is it a undefined behavior?```
#include <cstdio>
#include <utility>
#include <coroutine>
template<class T>
struct CRCoro {
struct RValueWrapper {
T* p;
operator T&&() const noexcept { return std::move(*p); }
};
using promise_type = T;
T& getDerived() noexcept { return *static_cast<T*>(this); }
auto get_return_object() noexcept { return RValueWrapper(&getDerived()); }
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_value(T&& x) noexcept { getDerived() = std::move(x); }
void unhandled_exception() {}
};
struct A : public CRCoro<A> {
int a;
};
A func() {
A aa{};
aa.a = 5;
co_return std::move(aa);
}
int main() {
printf("%d\n", func().a);
return 0;
}
```FYI:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118074https://github.com/llvm/llvm-project/issues/120200
The following code gives my expected result using MSVC, but a unpredictable result using GCC and clang. Is it a undefined behavior?```
#include <cstdio>
#include <utility>
#include <coroutine>
template<class T>
struct CRCoro {
struct RValueWrapper {
T* p;
operator T&&() const noexcept { return std::move(*p); }
};
using promise_type = T;
T& getDerived() noexcept { return *static_cast<T*>(this); }
auto get_return_object() noexcept { return RValueWrapper(&getDerived()); }
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_value(T&& x) noexcept { getDerived() = std::move(x); }
void unhandled_exception() {}
};
struct A : public CRCoro<A> {
int a;
};
A func() {
A aa{};
aa.a = 5;
co_return std::move(aa);
}
int main() {
printf("%d\n", func().a);
return 0;
}
```FYI:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118074https://github.com/llvm/llvm-project/issues/120200
Received on 2024-12-18 11:59:25