[Development] Views in APIs (was: Re: QString and related changes for Qt 6)

Иван Комиссаров abbapoh at gmail.com
Wed May 13 11:49:17 CEST 2020


Nope, that would not be C++ if it wasn’t broken:

class C1
{
public:
    std::string_view getMember() const & { return member; }
    std::string member;
};

std::string_view m = C1().getMember(); // compiles!

You have to delete the method explicitly:

std::string_view getMember() const && = delete;

Now compilation fails:

call to deleted member function 'getMember'
    std::string_view m = C1().getMember();
                         ~~~~~^~~~~~~~~

> 13 мая 2020 г., в 11:31, Marco Bubke <Marco.Bubke at qt.io> написал(а):
> 
>  
> auto view3 = myObject.getCopy()[42].getView(); // not safe, copy is destroyed
>  
> AFAIK you can say that a rvalue should not return a reference by using ref-qualified member functions.
>  
> View getView() const & { return v;}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20200513/7238b38f/attachment-0001.html>


More information about the Development mailing list