473,322 Members | 1,241 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,322 software developers and data experts.

Passing return as reference to another function

Hi all

I have the functions
friend CComplexMatrixTemp eye(const size_t nN);

and
friend CComplexMatrixTemp & chol(CComplexMatrixTemp &z);

I would like create expressions of the form CComplexMatrixTemp x =
chol(eye(6)), similar to MATLAB syntax.

GCC doesn't seem like the fact that chol(...) wants to take a reference to a
returned temporary. Is there anyway around this without losing the nice
notation? I could obviously do something like:
CComplexMatrixTemp temp = eye(80);
CComplexMatrixTemp x = chol(temp);
but this is definitely not as pretty!

Copying a matrix object is an expensive operation, hence the desire to use
references. The CComplexMatrixTemp class is formed in intermediate
expressions where storage space may be reused (I also have a CComplexMatrix
class which is used directly by the application programmer).

Thanks for any help!

Ryan
Jul 19 '05 #1
2 2978
"Ryan Mitchley" <rm******@removethis.worldonline.co.za> wrote...
I have the functions
friend CComplexMatrixTemp eye(const size_t nN);

and
friend CComplexMatrixTemp & chol(CComplexMatrixTemp &z);

I would like create expressions of the form CComplexMatrixTemp x =
chol(eye(6)), similar to MATLAB syntax.

GCC doesn't seem like the fact that chol(...) wants to take a reference to a returned temporary. Is there anyway around this without losing the nice
notation? I could obviously do something like:
CComplexMatrixTemp temp = eye(80);
CComplexMatrixTemp x = chol(temp);
but this is definitely not as pretty!

Copying a matrix object is an expensive operation, hence the desire to use
references. The CComplexMatrixTemp class is formed in intermediate
expressions where storage space may be reused (I also have a CComplexMatrix class which is used directly by the application programmer).


There is no way around the rule that a non-const reference cannot be
bound to a temporary. That said, there is no way for you to return
even a const reference to a local object (I assume 'eye' returns some
kind of automatic object it creates).

I think there are two solutions for you. Either return an object (as
you already do), make 'chol' accept a reference to a _constant_ object

CComplexMatrixTemp chol(CComplexMatrixTemp const &);

(and, apparently make it create another object too), _or_ you look over
the "MOJO" technique proposed by Andrei Alexandrescu and discussed in
several places, comp.lang.c++.moderated being probably the most
accessible to you.

Essentially, you might get away with inventing your own ref-counting
mechanism for matrix contents. Instead of the calculation data make
your matrix object store a pointer to them. Only when an matrix is
to change should it produce another calculation data and point to it.
This is called "copy on write". That way when you just return objects
from a function, the calculation data don't have to be reallocated.

All in all, you really shouldn't concern yourself with copying a matrix
object _until_ you see that you compiler cannot optimise it for you and
until you discover that it really slows everything down too much. There
is a significant optimisation allowed by the C++ Standard: return value
optimisation (RVO), which should be performed by most if not all modern
compilers.

Victor
Jul 19 '05 #2

VB> I think there are two solutions for you. Either return an object (as
VB> you already do), make 'chol' accept a reference to a _constant_ object

VB> CComplexMatrixTemp chol(CComplexMatrixTemp const &);

VB> (and, apparently make it create another object too), _or_ you look over
VB> the "MOJO" technique proposed by Andrei Alexandrescu and discussed in
VB> several places, comp.lang.c++.moderated being probably the most
VB> accessible to you.

Thanks for the detailed reply, Victor. I've actually come across an article
on the MOJO thing before. Unfortunately, I ran out of concentration about
half way through :-) Maybe it's time to look at it again . . .

VB> All in all, you really shouldn't concern yourself with copying a matrix
VB> object _until_ you see that you compiler cannot optimise it for you and
VB> until you discover that it really slows everything down too much.
VB> There is a significant optimisation allowed by the C++ Standard: return
VB> value optimisation (RVO), which should be performed by most if not all
VB> modern compilers.

Okay. I'm just not too sure how I'll tell when it's being done, and when
not.

Thanks again!

Ryan
Jul 19 '05 #3

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

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
22
by: tshad | last post by:
If I am passing a variable by reference to another routine by reference, do I need to dereference first? string testString; .... FirstSub(ref firstString) { HandleString(ref firstString); ...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
4
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.