![]() |
Does return-by-value mean extra copies and extra overhead?
Hi there,
I am reading: 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, |
Re: Does return-by-value mean extra copies and extra overhead?
On 4 Set, 15:36, mathieu <mathieu.malate...@gmail.com> wrote:
> Hi there, > > I am reading: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, 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 |
Re: Does return-by-value mean extra copies and extra overhead?
mathieu wrote:
> Hi there, > > I am reading: > 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, > > I am reading: > 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. Bo Persson |
| All times are GMT. The time now is 05:00 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.