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

Discussion in 'C++' started by mathieu, Sep 4, 2009.

  1. mathieu

    mathieu Guest

    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,
    mathieu, Sep 4, 2009
    #1
    1. Advertising

  2. mathieu

    Francesco Guest

    On 4 Set, 15:36, mathieu <> wrote:
    > 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
    Francesco, Sep 4, 2009
    #2
    1. Advertising

  3. Maxim Yegorushkin, Sep 4, 2009
    #3
  4. mathieu

    Bo Persson Guest

    mathieu wrote:
    > 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
    Bo Persson, Sep 4, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xerj
    Replies:
    4
    Views:
    350
  2. Greenhorn
    Replies:
    15
    Views:
    812
    Keith Thompson
    Mar 6, 2005
  3. Pete
    Replies:
    4
    Views:
    517
  4. alberttresens
    Replies:
    3
    Views:
    645
    Thomas Jollans
    Jul 26, 2010
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,198
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page