<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/98744>98744</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[coroutines] Wrong behavior of using get_return_object() when creating coroutines
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ddvamp
</td>
</tr>
</table>
<pre>
The standard says: "The expression promise.get_return_object() is used to initialize the returned reference or value result object of a call to a coroutine. The call to get_return_object is sequenced before the call to initial_suspend and is invoked at most once." [dcl.fct.def.coroutine]
However, if you look at the result of the following program https://godbolt.org/z/nY8Yo6vGs, it can be seen that the standard is not being followed.
Firstly, it is not the expression itself that is used for initialization, but its result
Secondly, if this result is a reference, it is still stored as a value
As a consequence, the behavior is not what the user expects. If we return an non-copyable and non-movable object type referece, we will get a compilation error. If the result contains references to the promise object, it may be destroyed before it is used. etc.
P.S. The same thing is observed in gcc
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VE2P4zYM_TXKhRgjkeN8HHyY7SBtbwW2QLGngSzRjrqy6Iq00-yvL2Q7k22LvSSwRL33yEfSMPsuItaq-qSqt40Z5Uqpdm4y_bBpyN3r368ILCY6kxywubMqX0Fpnc_x7yEhs6cIQ6LeMxYdyntCGVN8p-ZPtKL0SekzeIaR0YEQ-OjFm-C_IcgVYYlGBwlbTBgtAiWYTBjzHY9BYEECasGANSFkFAOWEo3iIxaQxTwu_qcgczP-NWZoBw22lBbmx4tV0DuPPGB0YKLLb3yc6Cs6MAI9sQBFi4XSGlT1ydlQtFYKh23xoUNVb2r7pravv9ANJ0xK_wS-hTuNEIi-ZqAl4SWpdv5qKQS6-djlEnbJ9HAVGXKVlb4ofenINRSkoNQpffmm9CV-OX2hw_Qzz_gC1kRoEBgxglxXkg_LPEMkgQYzxUKGrlh0XnxiCfcVZ42UfxvrhTG0C_DDxJbS00UjnmKGaEbJ0Wt-C8NntBTdSpFR_OM-g5mn508NLD4EYKGUa5-D5l5Y8JbfV57tjw9X8-Msu8GrmXwWt6Rye1RjZEw5KbTCBfzawu3Rd2AiRIovloa7aQLO5ueDnqb5e-0huQ-4ql34bgi3LLRDmcX0gw9zKQBTojSzfOe2pSjGR35mzLn1csQ6OvCYl7kQvblnUx2yJLo_-9Z_uFAAii2-L8tvxedlFNj0ucOz456BGsY0oQMfobN24-rSncuz2WC9O-rt6Vjtj4fNtS7LXeXs8bDT1p7L5qTd7njYYdm0e3cqy-PG13qr99vjrtzp6lgdivZsT21V2l2p9zvdoNpvsTc-FCFMfe7YjWcesT6fjvv9JpgGA8-rRuuIN5gvldZ586Q6v3lpxo7Vfhs8Cz9RxEuYd9THqLGq3uCPRLF7mk4tjJxz_tEOul0xgk1oJEc9sTZjCvV_ps7LdWwKS73Sl6xj_XsZEi2Il1k9K31Zsptq_U8AAAD__0jw1QU">