return value optimization

Discussion in 'C++' started by aaragon, Dec 4, 2007.

  1. aaragon

    aaragon Guest

    Hi,

    I'm designing a Matrix class so I read what Bjarne Stroustrup has on
    section 22.4.6 about it. It shows that it is possible to eliminate the
    temporaries by delaying the construction of the object. In his example
    (page 675):

    struct MVmul {
    const Matrix& m;
    const Vector& m;

    MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
    operator Vector(); // evaluate and return result
    };

    inline MVmul operator*(const Matrix& mm, const Vector& vv)
    {
    return MVmul(mm,vv);
    }

    Now, I think that's cool, however, I have a question about the
    converting function operator Vector(). The implementation could be
    done there OR it can be done as a constructor of the Vector class,
    right? Something like this:

    class Vector {
    // member variables
    ...
    public:
    // constructors
    ...
    Vector(const MVmul& mvmul) {
    // evaluate
    }
    ...
    // rest of the class
    };

    Now the question is, is an approach better than the other? I think
    that both are equivalent, but I wanted to make sure asking to the
    experts...

    Thank you,

    aaragon, Dec 4, 2007
    #1
    1. Advertising

  2. aaragon

    Kira Yamato Guest

    On 2007-12-04 00:26:52 -0500, aaragon <> said:

    > Hi,
    >
    > I'm designing a Matrix class so I read what Bjarne Stroustrup has on
    > section 22.4.6 about it. It shows that it is possible to eliminate the
    > temporaries by delaying the construction of the object. In his example
    > (page 675):
    >
    > struct MVmul {
    > const Matrix& m;
    > const Vector& m;
    >
    > MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
    > operator Vector(); // evaluate and return result
    > };
    >
    > inline MVmul operator*(const Matrix& mm, const Vector& vv)
    > {
    > return MVmul(mm,vv);
    > }
    >
    > Now, I think that's cool, however, I have a question about the
    > converting function operator Vector(). The implementation could be
    > done there OR it can be done as a constructor of the Vector class,
    > right? Something like this:
    >
    > class Vector {
    > // member variables
    > ...
    > public:
    > // constructors
    > ...
    > Vector(const MVmul& mvmul) {
    > // evaluate
    > }
    > ...
    > // rest of the class
    > };
    >
    > Now the question is, is an approach better than the other? I think
    > that both are equivalent, but I wanted to make sure asking to the
    > experts...


    Definitely in the MVmul class.

    --

    -kira
    Kira Yamato, Dec 4, 2007
    #2
    1. Advertising

  3. aaragon

    aaragon Guest

    On Dec 4, 2:05 am, Kira Yamato <> wrote:
    > On 2007-12-04 00:26:52 -0500, aaragon <> said:
    >
    >
    >
    > > Hi,

    >
    > > I'm designing a Matrix class so I read what Bjarne Stroustrup has on
    > > section 22.4.6 about it. It shows that it is possible to eliminate the
    > > temporaries by delaying the construction of the object. In his example
    > > (page 675):

    >
    > > struct MVmul {
    > > const Matrix& m;
    > > const Vector& m;

    >
    > > MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
    > > operator Vector(); // evaluate and return result
    > > };

    >
    > > inline MVmul operator*(const Matrix& mm, const Vector& vv)
    > > {
    > > return MVmul(mm,vv);
    > > }

    >
    > > Now, I think that's cool, however, I have a question about the
    > > converting function operator Vector(). The implementation could be
    > > done there OR it can be done as a constructor of the Vector class,
    > > right? Something like this:

    >
    > > class Vector {
    > > // member variables
    > > ...
    > > public:
    > > // constructors
    > > ...
    > > Vector(const MVmul& mvmul) {
    > > // evaluate
    > > }
    > > ...
    > > // rest of the class
    > > };

    >
    > > Now the question is, is an approach better than the other? I think
    > > that both are equivalent, but I wanted to make sure asking to the
    > > experts...

    >
    > Definitely in the MVmul class.
    >
    > --
    >
    > -kira


    Why?
    aaragon, Dec 4, 2007
    #3
  4. aaragon

    terminator Guest

    On Dec 4, 5:05 pm, aaragon <> wrote:
    > On Dec 4, 2:05 am, Kira Yamato <> wrote:
    >
    >
    >
    >
    >
    > > On 2007-12-04 00:26:52 -0500, aaragon <> said:

    >
    > > > Hi,

    >
    > > > I'm designing a Matrix class so I read what Bjarne Stroustrup has on
    > > > section 22.4.6 about it. It shows that it is possible to eliminate the
    > > > temporaries by delaying the construction of the object. In his example
    > > > (page 675):

    >
    > > > struct MVmul {
    > > > const Matrix& m;
    > > > const Vector& m;

    >
    > > > MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
    > > > operator Vector(); // evaluate and return result
    > > > };

    >
    > > > inline MVmul operator*(const Matrix& mm, const Vector& vv)
    > > > {
    > > > return MVmul(mm,vv);
    > > > }

    >
    > > > Now, I think that's cool, however, I have a question about the
    > > > converting function operator Vector(). The implementation could be
    > > > done there OR it can be done as a constructor of the Vector class,
    > > > right? Something like this:

    >
    > > > class Vector {
    > > > // member variables
    > > > ...
    > > > public:
    > > > // constructors
    > > > ...
    > > > Vector(const MVmul& mvmul) {
    > > > // evaluate
    > > > }
    > > > ...
    > > > // rest of the class
    > > > };

    >
    > > > Now the question is, is an approach better than the other? I think
    > > > that both are equivalent, but I wanted to make sure asking to the
    > > > experts...

    >
    > > Definitely in the MVmul class.

    >
    > > --

    >
    > > -kira

    >
    > Why?


    It(MVmul) has to convert its second argument to a Vector in either
    case.

    regards,
    FM.
    terminator, Dec 4, 2007
    #4
  5. aaragon

    aaragon Guest

    On Dec 4, 8:28 am, terminator <> wrote:
    > On Dec 4, 5:05 pm, aaragon <> wrote:
    >
    >
    >
    > > On Dec 4, 2:05 am, Kira Yamato <> wrote:

    >
    > > > On 2007-12-04 00:26:52 -0500, aaragon <> said:

    >
    > > > > Hi,

    >
    > > > > I'm designing a Matrix class so I read what Bjarne Stroustrup has on
    > > > > section 22.4.6 about it. It shows that it is possible to eliminate the
    > > > > temporaries by delaying the construction of the object. In his example
    > > > > (page 675):

    >
    > > > > struct MVmul {
    > > > > const Matrix& m;
    > > > > const Vector& m;

    >
    > > > > MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
    > > > > operator Vector(); // evaluate and return result
    > > > > };

    >
    > > > > inline MVmul operator*(const Matrix& mm, const Vector& vv)
    > > > > {
    > > > > return MVmul(mm,vv);
    > > > > }

    >
    > > > > Now, I think that's cool, however, I have a question about the
    > > > > converting function operator Vector(). The implementation could be
    > > > > done there OR it can be done as a constructor of the Vector class,
    > > > > right? Something like this:

    >
    > > > > class Vector {
    > > > > // member variables
    > > > > ...
    > > > > public:
    > > > > // constructors
    > > > > ...
    > > > > Vector(const MVmul& mvmul) {
    > > > > // evaluate
    > > > > }
    > > > > ...
    > > > > // rest of the class
    > > > > };

    >
    > > > > Now the question is, is an approach better than the other? I think
    > > > > that both are equivalent, but I wanted to make sure asking to the
    > > > > experts...

    >
    > > > Definitely in the MVmul class.

    >
    > > > --

    >
    > > > -kira

    >
    > > Why?

    >
    > It(MVmul) has to convert its second argument to a Vector in either
    > case.
    >
    > regards,
    > FM.


    No, that second argument is untouchable. Is passed as a constant
    reference. A new vector is created within the operator Vector()
    implementation. So I believe both approaches are equivalent.

    aaragon, Dec 4, 2007
    #5
  6. aaragon

    Kira Yamato Guest

    On 2007-12-04 17:29:50 -0500, aaragon <> said:

    > On Dec 4, 8:28 am, terminator <> wrote:
    >> On Dec 4, 5:05 pm, aaragon <> wrote:
    >>
    >>
    >>
    >>> On Dec 4, 2:05 am, Kira Yamato <> wrote:

    >>
    >>>> On 2007-12-04 00:26:52 -0500, aaragon <> sai

    > d:
    >>
    >>>>> Hi,

    >>
    >>>>> I'm designing a Matrix class so I read what Bjarne Stroustrup has on

    >
    >>>>> section 22.4.6 about it. It shows that it is possible to eliminate t

    > he
    >>>>> temporaries by delaying the construction of the object. In his examp

    > le
    >>>>> (page 675):

    >>
    >>>>> struct MVmul {
    >>>>> const Matrix& m;
    >>>>> const Vector& m;

    >>
    >>>>> MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
    >>>>> operator Vector(); // evaluate and return result
    >>>>> };

    >>
    >>>>> inline MVmul operator*(const Matrix& mm, const Vector& vv)
    >>>>> {
    >>>>> return MVmul(mm,vv);
    >>>>> }

    >>
    >>>>> Now, I think that's cool, however, I have a question about the
    >>>>> converting function operator Vector(). The implementation could be
    >>>>> done there OR it can be done as a constructor of the Vector class,
    >>>>> right? Something like this:

    >>
    >>>>> class Vector {
    >>>>> // member variables
    >>>>> ...
    >>>>> public:
    >>>>> // constructors
    >>>>> ...
    >>>>> Vector(const MVmul& mvmul) {
    >>>>> // evaluate
    >>>>> }
    >>>>> ...
    >>>>> // rest of the class
    >>>>> };

    >>
    >>>>> Now the question is, is an approach better than the other? I think
    >>>>> that both are equivalent, but I wanted to make sure asking to the
    >>>>> experts...

    >>
    >>>> Definitely in the MVmul class.

    >>
    >>>> --

    >>
    >>>> -kira

    >>
    >>> Why?

    >>
    >> It(MVmul) has to convert its second argument to a Vector in either
    >> case.
    >>
    >> regards,
    >> FM.

    >
    > No, that second argument is untouchable. Is passed as a constant
    > reference. A new vector is created within the operator Vector()
    > implementation. So I believe both approaches are equivalent.
    >
    > a²


    Equivalent in functionality, maybe. But certainly not in design.

    Just imagine what if in the near future, you want to introduce lazy
    evaluation for the inner product of two vectors VVinner.

    Next, think about what kind of changes is needed in both approaches you
    mentioned. And before you attempt to answer that question, first
    google up "idea behind encapsulation."

    --

    -kira
    Kira Yamato, Dec 5, 2007
    #6
  7. aaragon

    aaragon Guest

    On Dec 4, 6:05 pm, Kira Yamato <> wrote:
    > On 2007-12-04 17:29:50 -0500, aaragon <> said:
    >
    >
    >
    > > On Dec 4, 8:28 am, terminator <> wrote:
    > >> On Dec 4, 5:05 pm, aaragon <> wrote:

    >
    > >>> On Dec 4, 2:05 am, Kira Yamato <> wrote:

    >
    > >>>> On 2007-12-04 00:26:52 -0500, aaragon <> sai

    > > d:

    >
    > >>>>> Hi,

    >
    > >>>>> I'm designing a Matrix class so I read what Bjarne Stroustrup has on

    >
    > >>>>> section 22.4.6 about it. It shows that it is possible to eliminate t

    > > he
    > >>>>> temporaries by delaying the construction of the object. In his examp

    > > le
    > >>>>> (page 675):

    >
    > >>>>> struct MVmul {
    > >>>>> const Matrix& m;
    > >>>>> const Vector& m;

    >
    > >>>>> MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
    > >>>>> operator Vector(); // evaluate and return result
    > >>>>> };

    >
    > >>>>> inline MVmul operator*(const Matrix& mm, const Vector& vv)
    > >>>>> {
    > >>>>> return MVmul(mm,vv);
    > >>>>> }

    >
    > >>>>> Now, I think that's cool, however, I have a question about the
    > >>>>> converting function operator Vector(). The implementation could be
    > >>>>> done there OR it can be done as a constructor of the Vector class,
    > >>>>> right? Something like this:

    >
    > >>>>> class Vector {
    > >>>>> // member variables
    > >>>>> ...
    > >>>>> public:
    > >>>>> // constructors
    > >>>>> ...
    > >>>>> Vector(const MVmul& mvmul) {
    > >>>>> // evaluate
    > >>>>> }
    > >>>>> ...
    > >>>>> // rest of the class
    > >>>>> };

    >
    > >>>>> Now the question is, is an approach better than the other? I think
    > >>>>> that both are equivalent, but I wanted to make sure asking to the
    > >>>>> experts...

    >
    > >>>> Definitely in the MVmul class.

    >
    > >>>> --

    >
    > >>>> -kira

    >
    > >>> Why?

    >
    > >> It(MVmul) has to convert its second argument to a Vector in either
    > >> case.

    >
    > >> regards,
    > >> FM.

    >
    > > No, that second argument is untouchable. Is passed as a constant
    > > reference. A new vector is created within the operator Vector()
    > > implementation. So I believe both approaches are equivalent.

    >
    > > a²

    >
    > Equivalent in functionality, maybe. But certainly not in design.
    >
    > Just imagine what if in the near future, you want to introduce lazy
    > evaluation for the inner product of two vectors VVinner.
    >
    > Next, think about what kind of changes is needed in both approaches you
    > mentioned. And before you attempt to answer that question, first
    > google up "idea behind encapsulation."
    >
    > --
    >
    > -kira


    Well, you have a point there since I plan to have several of these
    little structures. However, the question behind my original post was
    referring to extra overhead from any of the both approaches, and
    unless someone else tells me otherwise, both are equivalent when
    creating the object. Right?

    aaragon, Dec 5, 2007
    #7
    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. E. Robert Tisdale
    Replies:
    0
    Views:
    825
    E. Robert Tisdale
    Jun 25, 2003
  2. Denis Remezov
    Replies:
    20
    Views:
    709
    Jakob Bieling
    Apr 20, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    780
    Keith Thompson
    Mar 6, 2005
  4. SzH
    Replies:
    10
    Views:
    635
    James Kanze
    Apr 26, 2007
  5. Ravikiran

    Zero Optimization and Sign Optimization???

    Ravikiran, Nov 17, 2008, in forum: C Programming
    Replies:
    22
    Views:
    831
    Thad Smith
    Nov 24, 2008
Loading...

Share This Page