Annex F (informative)
Core undefined behavior
[ub]
F.5
[dcl]
: Declarations
[ub.dcl]
F.5.6
[ub:dcl.fct.def.coroutine.resume.not.suspended]
Specified in:
[dcl.
fct.
def.
coroutine]
1
#
Invoking a resumption member function for a coroutine that is not suspended results in undefined behavior
.
2
#
[
Example
1
:
#include
<
coroutine
>
struct
minig
{
struct
promise_type
{
int
val; minig get_return_object
(
)
{
return
{
*
this
}
;
}
constexpr
suspend_always initial_suspend
(
)
noexcept
{
return
{
}
;
}
constexpr
suspend_always final_suspend
(
)
noexcept
{
return
{
}
;
}
constexpr
void
return_void
(
)
noexcept
{
}
[
[
noreturn
]
]
void
unhandled_exception
(
)
noexcept
{
throw
;
}
suspend_always yield_value
(
int
v
)
noexcept
{
val
=
v;
return
{
}
;
}
}
;
using
HDL
=
coroutine_handle
<
promise_type
>
; HDL coro; minig
(
promise_type
&
p
)
:
coro
(
HDL
::
from_promise
(
p
)
)
{
}
~
minig
(
)
{
coro
.
destroy
(
)
;
}
bool
move_next
(
)
{
coro
.
resume
(
)
;
return
!
coro
.
done
(
)
;
}
int
current_value
(
)
{
return
coro
.
promise
(
)
.
val;
}
}
;
static
minig f
(
int
n
)
{
for
(
int
i
=
0
; i
<
n;
+
+
i
)
co_yield
i;
}
int
main
(
)
{
auto
g
=
f
(
10
)
;
int
sum
=
0
;
while
(
g
.
move_next
(
)
)
sum
+
=
g
.
current_value
(
)
; g
.
move_next
(
)
;
// undefined behavior, will call
coro.resume()
but
final_
suspend
// has already returned, even though it returned
suspend_
always
return
sum;
}
—
end example
]