473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

return value optimization

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,


Dec 4 '07 #1
6 1819
On 2007-12-04 00:26:52 -0500, aaragon <al**************@gmail.comsaid:
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

Dec 4 '07 #2
On Dec 4, 2:05 am, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-04 00:26:52 -0500, aaragon <alejandro.ara...@gmail.comsaid:
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?
Dec 4 '07 #3
On Dec 4, 5:05 pm, aaragon <alejandro.ara...@gmail.comwrote:
On Dec 4, 2:05 am, Kira Yamato <kira...@earthlink.netwrote:


On 2007-12-04 00:26:52 -0500, aaragon <alejandro.ara...@gmail.comsaid:
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.
Dec 4 '07 #4
On Dec 4, 8:28 am, terminator <farid.mehr...@gmail.comwrote:
On Dec 4, 5:05 pm, aaragon <alejandro.ara...@gmail.comwrote:
On Dec 4, 2:05 am, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-04 00:26:52 -0500, aaragon <alejandro.ara...@gmail.comsaid:
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.


Dec 4 '07 #5
On 2007-12-04 17:29:50 -0500, aaragon <al**************@gmail.comsaid:
On Dec 4, 8:28 am, terminator <farid.mehr...@gmail.comwrote:
>On Dec 4, 5:05 pm, aaragon <alejandro.ara...@gmail.comwrote:
>>On Dec 4, 2:05 am, Kira Yamato <kira...@earthlink.netwrote:
>>>On 2007-12-04 00:26:52 -0500, aaragon <alejandro.ara...@gmail.comsai
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.

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

Dec 5 '07 #6
On Dec 4, 6:05 pm, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-04 17:29:50 -0500, aaragon <alejandro.ara...@gmail.comsaid:
On Dec 4, 8:28 am, terminator <farid.mehr...@gmail.comwrote:
On Dec 4, 5:05 pm, aaragon <alejandro.ara...@gmail.comwrote:
>On Dec 4, 2:05 am, Kira Yamato <kira...@earthlink.netwrote:
>>On 2007-12-04 00:26:52 -0500, aaragon <alejandro.ara...@gmail.comsai
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.

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?


Dec 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
14
by: Gama Franco | last post by:
Hi, I'm designing an interface for a shared library, and I would like to know if there is a standard about how to return an object to the user. I will use exceptions to report errors, so there...
29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
3
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
32
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
10
by: SzH | last post by:
The code below demonstrates that the copy constructor of moo is not called on the first line of main. In spite of this, g++ (version 4.1.2) refuses to compile it if I make the copy constructor...
18
by: terminator(jam) | last post by:
consider: struct memory_pig{//a really large type: memory_pig(){ std::cout<<"mem pig default\n"; //etc... }; memory_pig(memory_pig const&){
1
by: Rahul | last post by:
While reading "Efficient C++" by Dov Bulka I came across the follown statement "In addition, you must also define a copy constructor to "turn on" the Return Value Optimization(RVO). If the class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.