Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

c++ coroutine - winrt::fire_and_forget, What does this do?

I' m porting C++/CX application to C++/WinRT Core Application.

and I found a useful sample code (Simple3DGameDX) at this link.

https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/Simple3DGameDX/cppwinrt

and its Suspending method's return type is winrt::fire_and_forget. but anther example in C++/CX, its Suspending method's return type is void.

why this return type is not void in C++/WinRT? and What does this do?

C++/WinRT

winrt::fire_and_forget OnSuspending(IInspectable const& /* sender */, SuspendingEventArgs const& args)

C++/CX

void OnSuspending(Object^ Sender, SuspendingEventArgs^ Args)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

why this return type is not void in C++/WinRT? and What does this do?

Apart from documentation for Fire and forget, note the following: the function itself uses co_await operator in its body.

This requires that the function itself is coroutine friendly and could be compiled into "stackless" form, for asynchronous execution. void return type does not work out, but fire_and_forget struct is okay because C++/WinRT defines coroutine handling for it, as explained by the documentation.

Think of this as void which can be asynchronous and has no need to be waited on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...