[Development] Views in APIs

Thiago Macieira thiago.macieira at intel.com
Thu May 14 20:58:27 CEST 2020


On Thursday, 14 May 2020 02:33:43 PDT Marc Mutz via Development wrote:
> On 2020-05-14 02:15, Thiago Macieira wrote:
> > On quarta-feira, 13 de maio de 2020 01:41:26 PDT Иван Комиссаров wrote:
> >> std::span<Product> Project::productsSpan() const & { return
> >> d->products; }
> >> std::span<Product> Project::productsSpan() const && = delete;
> > 
> > Now implement either or both of these functions if internally you have
> > a
> > QSet<Products>.
> 
> Now implement QHash<Qt::ItemRole, QByteArray> roleNames() when
> internally you have a std::pmr::unordered_map<Qt::ItemRole,
> std::pmr::string>, with all the data allocated from one
> std::pmr::monotonic_buffer_resource.
> 
> This does both ways.

No, it doesn't. In both you created a copy on demand.

But you can only *return* said copy if the return type is an owning container. 
std::span and the views are not.

> And: you can also have views over associated data structures, they just
> can't have inline functions because they need to type-erase the
> container. For an extreme example, consider QAssociativeIterable.

Which is:
a) a horrible API
b) misspelling "iteratable"

To have type-erased containers, we need to go back to something like 
java.lang.Collection.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products





More information about the Development mailing list