|
20 | 20 |
|
21 | 21 | <meta name="viewport" content="width=device-width,initial-scale=1"> |
22 | 22 | <meta name="keywords" content=" |
23 | | - C++,標準ライブラリ,リファレンス,ドキュメント,STL,std,cpp20 |
| 23 | + C++,標準ライブラリ,リファレンス,ドキュメント,STL,std,cpp20,co_yield,co_await,co_return,promise_type,get_return_object,initial_suspend,final_suspend,yield_value,await_transform,return_value,return_void,unhandled_exception,get_return_object_on_allocation_failure,await_ready,await_suspend,await_resume |
24 | 24 | "> |
25 | 25 | <meta name="title" content="コルーチン [P0912R5] - cpprefjp C++日本語リファレンス" /> |
26 | 26 | <meta itemprop="name" content="コルーチン [P0912R5] - cpprefjp C++日本語リファレンス" /> |
|
68 | 68 |
|
69 | 69 | </head> |
70 | 70 | <body> |
71 | | -<header data-kunai-mdinfo="{"meta": {"cpp": ["cpp20"]}, "sources": [{"id": "c9af0c7cca23e5293be6f153073c33c7e5078492", "source": "#include <iostream>\n#include <coroutine>\n\n// \u30e1\u30e2\u30ea\u78ba\u4fdd\u304c\u5fc5\u8981\u3068\u306a\u3063\u305f\u3068\u304d\u306f::operator new(size_t, nothrow_t)\u304c\u4f7f\u308f\u308c\u308b\nstruct generator {\n struct promise_type;\n using handle = std::coroutine_handle<promise_type>;\n struct promise_type {\n int current_value;\n static auto get_return_object_on_allocation_failure() { return generator{nullptr}; }\n auto get_return_object() { return generator{handle::from_promise(*this)}; }\n auto initial_suspend() { return std::suspend_always{}; }\n auto final_suspend() noexcept { return std::suspend_always{}; }\n void unhandled_exception() { std::terminate(); }\n void return_void() {}\n auto yield_value(int value) {\n current_value = value;\n return std::suspend_always{};\n }\n };\n bool move_next() { return coro ? (coro.resume(), !coro.done()) : false; }\n int current_value() { return coro.promise().current_value; }\n generator(generator const&) = delete;\n generator(generator && rhs) : coro(rhs.coro) { rhs.coro = nullptr; }\n ~generator() { if (coro) coro.destroy(); }\nprivate:\n generator(handle h) : coro(h) {}\n handle coro;\n};\n\ngenerator f() { co_yield 1; co_yield 2; }\n\nint main() {\n auto g = f();\n while (g.move_next()) std::cout << g.current_value() << std::endl;\n}\n"}, {"id": "80c27bc07134b1355581a556e71f241e432142a1", "source": "#include <iostream>\n#include <coroutine>\n#include <utility>\n\n// \u30b3\u30eb\u30fc\u30c1\u30f3\u5229\u7528\u30e9\u30a4\u30d6\u30e9\u30ea: \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u578b\nstruct my_generator {\n // \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u308bPromise\u578b\n struct promise_type {\n // co_yield\u5f0f\u3067\u6307\u5b9a\u3055\u308c\u308bint\u5024\u3092\u4fdd\u6301\u3059\u308b\u5909\u6570\n int value_;\n\n auto get_return_object()\n {\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u306b\u7d10\u3065\u304fPromise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(*this)\u304b\u3089\n // \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u578b\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u623b\u308a\u5024\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\n return my_generator{*this};\n };\n auto initial_suspend()\n {\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u306e\u958b\u59cb\u524d\u306b\u7121\u6761\u4ef6\u30b5\u30b9\u30da\u30f3\u30c9\n return std::suspend_always{};\n }\n auto final_suspend() noexcept\n {\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u306e\u7d42\u4e86\u5f8c\u306b\u7121\u6761\u4ef6\u30b5\u30b9\u30da\u30f3\u30c9\n return std::suspend_always{};\n }\n auto yield_value(int v)\n {\n // co_yield\u5f0f\u3067\u6e21\u3055\u308c\u308b\u5024\u3092\u4fdd\u6301\u3057\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u7121\u6761\u4ef6\u30b5\u30b9\u30da\u30f3\u30c9\n value_ = v;\n return std::suspend_always{};\n }\n void return_void() {}\n void unhandled_exception() { std::terminate(); }\n };\n // \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u308b\u30b3\u30eb\u30fc\u30c1\u30f3\u30cf\u30f3\u30c9\u30eb\u578b\n using coro_handle = std::coroutine_handle<promise_type>;\n\n // \u7bc4\u56f2for\u69cb\u6587\u30b5\u30dd\u30fc\u30c8\u7528\u30a4\u30c6\u30ec\u30fc\u30bf\u578b\n struct iterator {\n // \u5bfe\u8c61\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u30cf\u30f3\u30c9\u30eb\n coro_handle coro_;\n // \u5bfe\u8c61\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u304c\u7d42\u4e86\u3057\u305f\u304b\u3092\u8868\u3059\u30d5\u30e9\u30b0\n bool done_;\n\n iterator& operator++()\n {\n // yield_value()\u3067\u4e2d\u65ad\u3057\u305f\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u518d\u958b\u3059\u308b\n coro_.resume();\n // (co_yield\u5f0f\u8a55\u4fa1\u3082\u3057\u304f\u306f\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u306e\u7d42\u4e86\u306b\u3088\u308a\u5236\u5fa1\u304c\u623b\u3063\u3066\u304f\u308b)\n done_ = coro_.done();\n return *this;\n }\n bool operator!=(const iterator& rhs) const\n {\n return done_ != rhs.done_;\n }\n int operator*() const\n {\n // Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u4fdd\u6301\u3057\u3066\u3044\u308b\u5024\u3092\u8fd4\u3059\n return coro_.promise().value_;\n }\n };\n\n ~my_generator()\n {\n if (coro_)\n coro_.destroy();\n }\n\n my_generator(my_generator const&) = delete;\n my_generator(my_generator&& rhs) \n : coro_(std::exchange(rhs.coro_, nullptr)) {}\n\n // \u7bc4\u56f2for\u69cb\u6587\u30b5\u30dd\u30fc\u30c8\u7528\u306e\u30e1\u30f3\u30d0\u95a2\u6570\n iterator begin()\n {\n // initial_suspend()\u3067\u4e2d\u65ad\u3057\u305f\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u518d\u958b\u3059\u308b\n coro_.resume();\n // (\u521d\u56deco_yield\u5f0f\u8a55\u4fa1\u306b\u3088\u308a\u5236\u5fa1\u304c\u623b\u3063\u3066\u304f\u308b)\n return {coro_, coro_.done()};\n }\n iterator end()\n {\n // \u7d42\u7aef\u4f4d\u7f6e\u3092\u8868\u73fe\u3059\u308b\u756a\u5175\u30a4\u30c6\u30ec\u30fc\u30bf\n return {{}, true};\n }\n\nprivate:\n // Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u7d4c\u7531\u3067\u30b3\u30eb\u30fc\u30c1\u30f3\u30cf\u30f3\u30c9\u30eb\u3092\u53d6\u5f97\u3059\u308b\n explicit my_generator(promise_type& p)\n : coro_(coro_handle::from_promise(p)) {}\n\n coro_handle coro_;\n};\n\n\n// \u30e6\u30fc\u30b6\u5b9a\u7fa9\u30b3\u30eb\u30fc\u30c1\u30f3\nmy_generator iota(int end)\n{\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u306b\u5bfe\u5fdc\u3057\u305fPromise\u578b generator::promise_type\u306e\n // Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(p)\u304c\u751f\u6210\u3055\u308c\u308b\u3002\n\n for (int n = 0; n < end; ++n) {\n // \u4e0b\u5f0f\u306f co_await p.yield_value(n) \u3068\u7b49\u4fa1\n co_yield n;\n }\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u306e\u7d42\u7aef\u5230\u9054\u306b\u3088\u308a p.return_void() \u547c\u3073\u51fa\u3057\n}\n\nint main()\n{\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u547c\u3073\u51fa\u3057\u3001\u6574\u6570\u751f\u6210\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3002\n auto g = iota(10);\n // \u3053\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u306f\u307e\u3060\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u306f\u5b9f\u884c\u3055\u308c\u306a\u3044\u3002\n\n // \u7bc4\u56f2for\u69cb\u6587\u3092\u7528\u3044\u3066\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u3092\u5b9f\u884c\u3059\u308b\u3002\n // \u3053\u3053\u3067\u306f\u30b3\u30eb\u30fc\u30c1\u30f3iota\u306e\u5024\u751f\u6210\u30eb\u30fc\u30d7\u51e6\u7406\u30b9\u30c6\u30c3\u30d7\u3068\u3001\n // main\u95a2\u6570\u306e\u8868\u793a\u30eb\u30fc\u30d7\u51e6\u7406\u30b9\u30c6\u30c3\u30d7\u304c\u4ea4\u4e92\u306b\u5b9f\u884c\u3055\u308c\u308b\u3002\n for (int v: g) {\n std::cout << v;\n }\n}\n"}], "page_id": ["lang", "cpp20", "coroutines"]}"> |
| 71 | +<header data-kunai-mdinfo="{"meta": {"cpp": ["cpp20"], "alias": ["co_yield,co_await,co_return,promise_type,get_return_object,initial_suspend,final_suspend,yield_value,await_transform,return_value,return_void,unhandled_exception,get_return_object_on_allocation_failure,await_ready,await_suspend,await_resume"]}, "sources": [{"id": "c9af0c7cca23e5293be6f153073c33c7e5078492", "source": "#include <iostream>\n#include <coroutine>\n\n// \u30e1\u30e2\u30ea\u78ba\u4fdd\u304c\u5fc5\u8981\u3068\u306a\u3063\u305f\u3068\u304d\u306f::operator new(size_t, nothrow_t)\u304c\u4f7f\u308f\u308c\u308b\nstruct generator {\n struct promise_type;\n using handle = std::coroutine_handle<promise_type>;\n struct promise_type {\n int current_value;\n static auto get_return_object_on_allocation_failure() { return generator{nullptr}; }\n auto get_return_object() { return generator{handle::from_promise(*this)}; }\n auto initial_suspend() { return std::suspend_always{}; }\n auto final_suspend() noexcept { return std::suspend_always{}; }\n void unhandled_exception() { std::terminate(); }\n void return_void() {}\n auto yield_value(int value) {\n current_value = value;\n return std::suspend_always{};\n }\n };\n bool move_next() { return coro ? (coro.resume(), !coro.done()) : false; }\n int current_value() { return coro.promise().current_value; }\n generator(generator const&) = delete;\n generator(generator && rhs) : coro(rhs.coro) { rhs.coro = nullptr; }\n ~generator() { if (coro) coro.destroy(); }\nprivate:\n generator(handle h) : coro(h) {}\n handle coro;\n};\n\ngenerator f() { co_yield 1; co_yield 2; }\n\nint main() {\n auto g = f();\n while (g.move_next()) std::cout << g.current_value() << std::endl;\n}\n"}, {"id": "80c27bc07134b1355581a556e71f241e432142a1", "source": "#include <iostream>\n#include <coroutine>\n#include <utility>\n\n// \u30b3\u30eb\u30fc\u30c1\u30f3\u5229\u7528\u30e9\u30a4\u30d6\u30e9\u30ea: \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u578b\nstruct my_generator {\n // \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u308bPromise\u578b\n struct promise_type {\n // co_yield\u5f0f\u3067\u6307\u5b9a\u3055\u308c\u308bint\u5024\u3092\u4fdd\u6301\u3059\u308b\u5909\u6570\n int value_;\n\n auto get_return_object()\n {\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u306b\u7d10\u3065\u304fPromise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(*this)\u304b\u3089\n // \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u578b\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u623b\u308a\u5024\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\n return my_generator{*this};\n };\n auto initial_suspend()\n {\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u306e\u958b\u59cb\u524d\u306b\u7121\u6761\u4ef6\u30b5\u30b9\u30da\u30f3\u30c9\n return std::suspend_always{};\n }\n auto final_suspend() noexcept\n {\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u306e\u7d42\u4e86\u5f8c\u306b\u7121\u6761\u4ef6\u30b5\u30b9\u30da\u30f3\u30c9\n return std::suspend_always{};\n }\n auto yield_value(int v)\n {\n // co_yield\u5f0f\u3067\u6e21\u3055\u308c\u308b\u5024\u3092\u4fdd\u6301\u3057\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u7121\u6761\u4ef6\u30b5\u30b9\u30da\u30f3\u30c9\n value_ = v;\n return std::suspend_always{};\n }\n void return_void() {}\n void unhandled_exception() { std::terminate(); }\n };\n // \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u308b\u30b3\u30eb\u30fc\u30c1\u30f3\u30cf\u30f3\u30c9\u30eb\u578b\n using coro_handle = std::coroutine_handle<promise_type>;\n\n // \u7bc4\u56f2for\u69cb\u6587\u30b5\u30dd\u30fc\u30c8\u7528\u30a4\u30c6\u30ec\u30fc\u30bf\u578b\n struct iterator {\n // \u5bfe\u8c61\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u30cf\u30f3\u30c9\u30eb\n coro_handle coro_;\n // \u5bfe\u8c61\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u304c\u7d42\u4e86\u3057\u305f\u304b\u3092\u8868\u3059\u30d5\u30e9\u30b0\n bool done_;\n\n iterator& operator++()\n {\n // yield_value()\u3067\u4e2d\u65ad\u3057\u305f\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u518d\u958b\u3059\u308b\n coro_.resume();\n // (co_yield\u5f0f\u8a55\u4fa1\u3082\u3057\u304f\u306f\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u51e6\u7406\u306e\u7d42\u4e86\u306b\u3088\u308a\u5236\u5fa1\u304c\u623b\u3063\u3066\u304f\u308b)\n done_ = coro_.done();\n return *this;\n }\n bool operator!=(const iterator& rhs) const\n {\n return done_ != rhs.done_;\n }\n int operator*() const\n {\n // Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u4fdd\u6301\u3057\u3066\u3044\u308b\u5024\u3092\u8fd4\u3059\n return coro_.promise().value_;\n }\n };\n\n ~my_generator()\n {\n if (coro_)\n coro_.destroy();\n }\n\n my_generator(my_generator const&) = delete;\n my_generator(my_generator&& rhs) \n : coro_(std::exchange(rhs.coro_, nullptr)) {}\n\n // \u7bc4\u56f2for\u69cb\u6587\u30b5\u30dd\u30fc\u30c8\u7528\u306e\u30e1\u30f3\u30d0\u95a2\u6570\n iterator begin()\n {\n // initial_suspend()\u3067\u4e2d\u65ad\u3057\u305f\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u518d\u958b\u3059\u308b\n coro_.resume();\n // (\u521d\u56deco_yield\u5f0f\u8a55\u4fa1\u306b\u3088\u308a\u5236\u5fa1\u304c\u623b\u3063\u3066\u304f\u308b)\n return {coro_, coro_.done()};\n }\n iterator end()\n {\n // \u7d42\u7aef\u4f4d\u7f6e\u3092\u8868\u73fe\u3059\u308b\u756a\u5175\u30a4\u30c6\u30ec\u30fc\u30bf\n return {{}, true};\n }\n\nprivate:\n // Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u7d4c\u7531\u3067\u30b3\u30eb\u30fc\u30c1\u30f3\u30cf\u30f3\u30c9\u30eb\u3092\u53d6\u5f97\u3059\u308b\n explicit my_generator(promise_type& p)\n : coro_(coro_handle::from_promise(p)) {}\n\n coro_handle coro_;\n};\n\n\n// \u30e6\u30fc\u30b6\u5b9a\u7fa9\u30b3\u30eb\u30fc\u30c1\u30f3\nmy_generator iota(int end)\n{\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u306b\u5bfe\u5fdc\u3057\u305fPromise\u578b generator::promise_type\u306e\n // Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(p)\u304c\u751f\u6210\u3055\u308c\u308b\u3002\n\n for (int n = 0; n < end; ++n) {\n // \u4e0b\u5f0f\u306f co_await p.yield_value(n) \u3068\u7b49\u4fa1\n co_yield n;\n }\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u306e\u7d42\u7aef\u5230\u9054\u306b\u3088\u308a p.return_void() \u547c\u3073\u51fa\u3057\n}\n\nint main()\n{\n // \u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u547c\u3073\u51fa\u3057\u3001\u6574\u6570\u751f\u6210\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3002\n auto g = iota(10);\n // \u3053\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u306f\u307e\u3060\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u306f\u5b9f\u884c\u3055\u308c\u306a\u3044\u3002\n\n // \u7bc4\u56f2for\u69cb\u6587\u3092\u7528\u3044\u3066\u30b3\u30eb\u30fc\u30c1\u30f3\u672c\u4f53\u3092\u5b9f\u884c\u3059\u308b\u3002\n // \u3053\u3053\u3067\u306f\u30b3\u30eb\u30fc\u30c1\u30f3iota\u306e\u5024\u751f\u6210\u30eb\u30fc\u30d7\u51e6\u7406\u30b9\u30c6\u30c3\u30d7\u3068\u3001\n // main\u95a2\u6570\u306e\u8868\u793a\u30eb\u30fc\u30d7\u51e6\u7406\u30b9\u30c6\u30c3\u30d7\u304c\u4ea4\u4e92\u306b\u5b9f\u884c\u3055\u308c\u308b\u3002\n for (int v: g) {\n std::cout << v;\n }\n}\n"}], "page_id": ["lang", "cpp20", "coroutines"]}"> |
72 | 72 | <nav class="navbar navbar-default" role="navigation"> |
73 | 73 | <div class="container-fluid"> |
74 | 74 | <div class="navbar-header"> |
|
188 | 188 |
|
189 | 189 | <p class="text-right"><small> |
190 | 190 | 最終更新日時(UTC): |
191 | | - <span itemprop="datePublished" content="2026-01-06T03:47:59"> |
192 | | - 2026年01月06日 03時47分59秒 |
| 191 | + <span itemprop="datePublished" content="2026-01-16T06:50:25"> |
| 192 | + 2026年01月16日 06時50分25秒 |
193 | 193 | </span> |
194 | 194 | <br/> |
195 | 195 | <span itemprop="author" itemscope itemtype="http://schema.org/Person"> |
196 | | - <span itemprop="name">Koichi Murase</span> |
| 196 | + <span itemprop="name">yoh</span> |
197 | 197 | </span> |
198 | 198 | が更新 |
199 | 199 | </small></p> |
|
214 | 214 |
|
215 | 215 | <h1 itemprop="name"><span class="token">コルーチン [P0912R5]</span><span class="cpp cpp20" title="C++20で追加">(C++20)</span></h1> |
216 | 216 | <div itemprop="articleBody"><p></p> |
| 217 | +<p></p> |
217 | 218 | <p>このページはC++20に採用された言語機能の変更を解説しています。</p> |
218 | 219 | <p>のちのC++規格でさらに変更される場合があるため<a href="#relative-page">関連項目</a>を参照してください。</p> |
219 | 220 | <p></p> |
|
0 commit comments