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

M

mathieu

Hi there,

I am reading:
http://www.parashift.com/c++-faq-lite/ctors.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,
 
F

Francesco

Hi there,

I am reading:http://www.parashift.com/c++-faq-lite/ctors.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,

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
overhead.

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
 
B

Bo Persson

mathieu said:
Hi there,

I am reading:
http://www.parashift.com/c++-faq-lite/ctors.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.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top