std::coroutine_traits

From cppreference.com
< cpp‎ | coroutine
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
Coroutine support
Coroutine traits
coroutine_traits
(C++20)
Coroutine handle
No-op coroutines
Trivial awaitables
 
Defined in header <coroutine>
template< class R, class... Args >
struct coroutine_traits;
(since C++20)

Determines the promise type from the return type and parameter types of a coroutine. The standand library implementation provides a publicly accessible member type promise_type same as R::promise_type if the qualified-id is valid and denotes a type. Otherwise, it has no such member.

Program-defined specializations of coroutine_traits shall define a publicly accessible member type promise_type; otherwise, the behavior is undefined.

Template parameters

R - return type of the coroutine
Args - parameter types of the coroutine, including the implicit object parameter if the coroutine is a non-static member function

Member types

Type Definition
promise_type R::promise_type if it is valid, or provided by program-defined specializations

Possible implementation

template<class, class...>
struct coroutine_traits {};
 
template<class R, class... Args>
    requires requires { typename R::promise_type }
struct coroutine_traits<R, Args...> {
    using promise_type = R::promise_type;
};

Notes

If the coroutine is a non-static member function, then the first type in Args... is the type of the implicit object parameter, and the rest are parameter types of the function (if any).

If std::coroutine_traits<R, Args...>::promise_type does not exist or is not a class type, the corresponding coroutine definition is ill-formed.

Users may define explicit or partial specializations of coroutine_traits dependent on program-defined types to avoid modification to return types.

Example