title: C++ coroutines: call a coroutine function without co_await
body: <p>struct task is modified from <a href="https://github.com/Quuxplusone/coro/blob/master/include/coro/gor_task.h" rel="nofollow noreferrer">https://github.com/Quuxplusone/coro/blob/master/include/coro/gor_task.h</a>. I just change <code>suspend_always</code> <a href="https://github.com/Quuxplusone/coro/blob/master/include/coro/gor_task.h#L18" rel="nofollow noreferrer">in Line 18</a> to suspend_never.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// test.cpp
  #include &lt;exception&gt;
  #include &lt;experimental/coroutine&gt;
  #include &lt;variant&gt;
  
  template&lt;class T&gt;
  struct task {
      struct promise_type {
          std::variant&lt;std::monostate, T, std::exception_ptr&gt; result_;
          std::experimental::coroutine_handle&lt;void&gt; waiter_;
  
          task get_return_object() { return task(this); }
          auto initial_suspend() { return std::experimental::suspend_never{}; } // Originally suspend_always
          auto final_suspend() {
              struct Awaiter {
                  promise_type *me_;
                  bool await_ready() { return false; }
                  void await_suspend(std::experimental::coroutine_handle&lt;void&gt; caller) {
                      me_-&gt;waiter_.resume();
                  }
                  void await_resume() {}
              };
              return Awaiter{this};
          }
          template&lt;class U&gt;
          void return_value(U&amp;&amp; u) {
              result_.template emplace&lt;1&gt;(static_cast&lt;U&amp;&amp;&gt;(u));
          }
          void unhandled_exception() {
              result_.template emplace&lt;2&gt;(std::current_exception());
          }
      };
  
      bool await_ready() { return false; }
      void await_suspend(std::experimental::coroutine_handle&lt;void&gt; caller) {
          coro_.promise().waiter_ = caller;
          coro_.resume();
      }
      T await_resume() {
          if (coro_.promise().result_.index() == 2) {
              std::rethrow_exception(std::get&lt;2&gt;(coro_.promise().result_));
          }
          return std::get&lt;1&gt;(coro_.promise().result_);
      }
  
      ~task() {
          coro_.destroy();
      }
  private:
      using handle_t = std::experimental::coroutine_handle&lt;promise_type&gt;;
      task(promise_type *p) : coro_(handle_t::from_promise(*p)) {}
      handle_t coro_;
  };
  
  #include &lt;stdio.h&gt;
  
  task&lt;int&gt; f2() {
      puts("enter f2");
      co_return 1;
  }
  
  task&lt;int&gt; f1() {
      puts("enter f1");
      int a = co_await f2();
      printf("f2 return: %d\n", a);
      co_return a;
  }
  
  int main() {
      f1();
  }
  </code></pre>
  
  <pre class="lang-sh prettyprint-override"><code>$ clang++ -fcoroutines-ts -std=c++17 -stdlib=libc++ -lc++ -lc++abi test.cpp -o test
  $ ./test
  enter f1
  enter f2
  fish: './test' terminated by signal SIGSEGV (Address boundary error)
  </code></pre>
  
  <p>Because there's no <code>co_await</code>, I expect <code>f2 return: 1</code> should be printed and the program should exit normally, but it crashes with segfault. Why and how can I fix this issue?</p>