[Development] QList
Marc Mutz
marc.mutz at kdab.com
Fri Mar 24 09:34:18 CET 2017
Hi Thiago,
You misunderstand me.
I listed _the_ three use-cases where inheritance is the tool of choice:
1. modelling is-a
2. inheriting to reuse
3. reimplementing virtual functions
If your use-case is not one of the three, then inheritance is not the right
tool.
Clearly, by now, it's neither (1) nor (3). So if anything, it needs to be (2).
And I'm arguing that it's not (2), either, because you can only re-use a
subset of QStringView API, and need to wrap the other half anyway. (2) is
really only interesting for pure OO languages with no concept of free
functions. In C++, we have free functions.
On Thursday 23 March 2017 22:22:36 Thiago Macieira wrote:
> > > A naïve implementation of QString::mid() could be:
> > >
> > >
> > > QString QString::mid(int start, int len) const
> > > {
> > > return QString(QStringView::mid(start, len));
> > > }
> >
> > Which is no different from
> >
> > return QString(QStringView(*this).mid(start, len));
> >
> >
> > so does not provide a case supporting using inhertance.
>
> Actually, the code generation could be slightly different. If
> QStringView::mid() isn't inlined, then the difference is whether we need
> to create a temporary QStringView in the stack and pass its this pointer
> as a parameter.
If QStringView::mid() isn't inline, then it doesn't matter which
implementation we choose. The implicit this in QStringView::mid is still
there, forcing the object on the stack.
So what I take away from this discussion is "avoid having to go though *this
(into out-of-line code)". And to avoid having to go through *this, you need
free functions, not member functions of some private base class.
And this is what I intend to do: QStringView::mid is inline. And I'm proposing
to make all QString/View member functions inline non-exported, either
implemented directly or by calling a free function (exported or not).
As an aside: going through *this has obviously so far not been an issue with
QString's myriads of member functions, so I guess even on Win64 calling
performance will increase with QStringView taken by value.
Ville: are there any proposals for making *this passable by value? Like
struct QLineF {
Q_CORE_EXPORT QPointF intersection(QLineF other) =;
// =: pass *this by value, mimicking & and && for (l|r)value ref
};
Thanks,
Marc
--
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel: +49-30-521325470
KDAB - The Qt, C++ and OpenGL Experts
More information about the Development
mailing list