return value optimization

A

aaragon

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,

 
K

Kira Yamato

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

aaragon

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.

 
K

Kira Yamato

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.


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."
 
A

aaragon

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

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?

 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top