Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)

 mathieu 09-04-2009 01:36 PM

Does return-by-value mean extra copies and extra overhead?

Hi there,

http://www.parashift.com/c++-faq-lit....html#faq-10.9

And I am thinking does the opposite also apply? For instance:

template<typename T>
struct minimum : std::binary_function<T, T, T>
{
const T& operator()(const T& x, const T& y) const { return x < y?
x : y; }
};

Clearly for integer type such as int, there is not need to construct
a reference, but instead it would be faster (I would think) to simply
do:

template<typename T>
struct minimum : std::binary_function<T, T, T>
{
T operator()(T x, T y) const { return x < y? x : y; }
};

Thanks,

 Francesco 09-04-2009 03:05 PM

Re: Does return-by-value mean extra copies and extra overhead?

On 4 Set, 15:36, mathieu <mathieu.malate...@gmail.com> wrote:
> Hi there,
>
>
> And I am thinking does the opposite also apply? For instance:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> const T& operator()(const T& x, const T& y) const { return x < y?
> x : y; }
> };
>
> Clearly for integer type such as int, there is not need to construct
> a reference, but instead it would be faster (I would think) to simply
> do:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> T operator()(T x, T y) const { return x < y? x : y; }
> };
>
> Thanks,

I think the actual results depend strongly on the implementation. You
could profile it and see by yourself if passing ints by value is
really faster than passing them by reference. After all, some
implementations could define int to have the same number of bits of
addresses, then you wouldn't be saving anything.

In any case, implementing a by-value template function like the above
you should ensure that it only gets used for built-in types -
otherwise, passing large objects to it will _surely_ cause significant

Well, there are specializations for doing this. Define a by-reference
template, then specialize it by-value for int (once you have verified
that it really is faster).

Don't forget to declare your templates as inline - maybe it won't
help, but surely it won't harm.

Cheers,
Francesco

 Maxim Yegorushkin 09-04-2009 03:33 PM

Re: Does return-by-value mean extra copies and extra overhead?

mathieu wrote:
> Hi there,
>
> http://www.parashift.com/c++-faq-lit....html#faq-10.9
>
> And I am thinking does the opposite also apply? For instance:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> const T& operator()(const T& x, const T& y) const { return x < y?
> x : y; }
> };

There is an excellent article by David Abrahams on this subject:
http://cpp-next.com/archive/2009/08/...pass-by-value/

--
Max

Re: Does return-by-value mean extra copies and extra overhead?

mathieu wrote:
> Hi there,
>
> http://www.parashift.com/c++-faq-lit....html#faq-10.9
>
> And I am thinking does the opposite also apply? For instance:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> const T& operator()(const T& x, const T& y) const { return x < y?
> x : y; }
> };
>
> Clearly for integer type such as int, there is not need to
> construct a reference, but instead it would be faster (I would
> think) to simply do:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> T operator()(T x, T y) const { return x < y? x : y; }
> };
>

Any competent compiler will inline functions this simple, and make the
difference disappear. You shouldn't bother.