[Development] RFC: Containers member functions for algorithm

Thiago Macieira thiago.macieira at intel.com
Thu Mar 23 21:36:41 CET 2017


On quinta-feira, 23 de março de 2017 02:36:06 PDT Marc Mutz wrote:
> Second, that member functions always get their *this argument passed by
> reference. A free function can take the argument by value instead.

The example for this is actually in the other thread: QStringView::indexOf().

If you write:
	QStringView(u"Hello").indexOf(u'l');

The compiler has to load the address of the u"Hello" literal and the size (5), 
then save them to a temporary in the stack, then load the address of that 
temporary into the register and call the target function.

A free function passing QStringView by value would (not on Windows[*]) pass 
the address of the literal and the length as parameters. Compare the f and g 
functions in https://godbolt.org/g/lV5Swu.

How to solve this? Easy, inline indexOf redirecting the call to the free 
function, as in https://godbolt.org/g/GFRxKU.

[*] Why not on Windows? Change the compiler to Clang and add
	-target x86_64-mingw
you'll see that now f and g are identical in both cases.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Development mailing list