intcounter(void){ staticint i, state = 0; switch (state) { case0: /* start of function */ for (i = 0; i < 10; i++) { state = 1; /* so we will come back to "case 1" */ return i; case1:; /* resume control straight after the return */ } } } intmain() { for (int i = 0; i < 10; i++) printf("%d\n", counter()); }
Task coroutineDemo(int times) { for (size_t i = 0; i < times; i++) { cout << "coroutineDemo\n"; co_await suspend_always{}; } co_return; }
intmain(){ auto task = coroutineDemo(3); while (!task.done()) { task.resume(); } std::cout << "Done\n"; return0; }
結果不如預期,發現問題出在
Once execution propagates outside of the coroutine body then the coroutine frame is destroyed. Destroying the coroutine frame involves a number of steps:
Call the destructor of the promise object.
Call the destructors of the function parameter copies.
Call operator delete to free the memory used by the coroutine frame (optional)
public: staticboolconst value = sizeof(Test<T>(0)) == sizeof(Yes); };
template<bool B, classT = void> // Defaulttemplateversion. structenable_if {}; // This struct doesn't define "type" and the substitution will fail if you try to access it.
template<classT> // Aspecialisationusediftheexpressionistrue. structenable_if<true, T> {typedef T type; }; // This struct do have a "type" and won't fail on access.
template<typename T> concept have_value = requires(T a) { a.value; }; template<typename T> requires have_value<T> autoget_value(const T& a) { auto v = a.value; // do some thing return v; } intmain() { int b = 10; auto y = get_value(b); }
錯誤訊息如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<source>:2:1: error: 'concept' does not name a type 2 | concept have_value = requires(T a) { | ^~~~~~~
<source>:2:1: note: 'concept' only available with '-fconcepts' <source>:7:1: error: 'requires' does not name a type 7 | requires have_value<T> | ^~~~~~~~
<source>: In function'int main()': <source>:18:12: error: 'get_value' was not declared in this scope 18 | auto y = get_value(b); | ^~~~~~~~~
module.exports = { parser: "@typescript-eslint/parser", // Specifies the ESLint parser extends: [ "plugin:@typescript-eslint/recommended"// Uses the recommended rules from the @typescript-eslint/eslint-plugin ], parserOptions: { ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features sourceType: "module"// Allows for the use of imports }, rules: { // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs // e.g. "@typescript-eslint/explicit-function-return-type": "off", } };
module.exports = { parser: "@typescript-eslint/parser", // Specifies the ESLint parser extends: [ "plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin "prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier "plugin:prettier/recommended"// Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array. ], parserOptions: { ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features sourceType: "module"// Allows for the use of imports } };