Hacker News new | ask | show | jobs
by dmeybohm 911 days ago
Yeah I agree, the ListView naming is more appropriate.

I haven't used non-owning types like string_view or span too much because I haven't needed that level of performance or memory optimization yet, and so those just seem like footguns as compared to just a reference without those needs. I do like to use a technique in classes that use non-owning references that would work for those too to prevent this particular problem.

For that, there are two methods with the same name, but different access - an lvalue version and an rvalue version. Then, you delete the rvalue method like this:

  class Response {
    auto getListView() & -> ListView {
      return ListView(m_List);
    }
    void getListView() && = delete;
  }
Then you get a compile error like in Rust when you try to call getListView() from a temporary object, but if you call the method from an lvalue it still works at least as long as the object is in scope.