[Development] Passing QSize, QPoint, QTime and other small structs by value
Marc Mutz
marc.mutz at kdab.com
Tue Apr 11 09:26:44 CEST 2017
On Tuesday 11 April 2017 07:36:52 Thiago Macieira wrote:
> Em segunda-feira, 10 de abril de 2017, às 13:27:05 PDT, Matthew Woehlke
>
> escreveu:
> > Option 2: Teach C++ to choose automatically if such types should be
> > passed by value or by const-reference.
>
> That's what I did to QVector.
>
> typedef typename std::conditional<std::is_fundamental<T>::value ||
> std::is_pointer<T>::value, T, const T &>::type parameter_type;
>
> value_type value(int i, parameter_type defaultValue) const
> Q_DECL_NOEXCEPT { return size_t(i) < d->size ? at(i) : defaultValue; }
Boost::call_traits::param_type and the above, however, only work in in non-
deduced contexts. Iow: you cannot use it in a hypothetical
typename <Arithmetic A>
auto sin(boost::call_traits<A>::param_type x);
auto sinX = sin(1.0); // ERROR: cannot deduce 'A'
(this is another reason why still today one should write function templates as
a pair of general template plus op()-in-template-class: you make the general
template Q_ALWAYS_INLINE, and can specialize the class template's argument
passing any way you like).
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