473,785 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing return as reference to another function

Hi all

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

and
friend CComplexMatrixT emp & chol(CComplexMa trixTemp &z);

I would like create expressions of the form CComplexMatrixT emp 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:
CComplexMatrixT emp temp = eye(80);
CComplexMatrixT emp 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 CComplexMatrixT emp 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 3006
"Ryan Mitchley" <rm******@remov ethis.worldonli ne.co.za> wrote...
I have the functions
friend CComplexMatrixT emp eye(const size_t nN);

and
friend CComplexMatrixT emp & chol(CComplexMa trixTemp &z);

I would like create expressions of the form CComplexMatrixT emp 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:
CComplexMatrixT emp temp = eye(80);
CComplexMatrixT emp 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 CComplexMatrixT emp 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

CComplexMatrixT emp chol(CComplexMa trixTemp 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++.m oderated 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> CComplexMatrixT emp chol(CComplexMa trixTemp 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++.m oderated 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
3601
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 in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
5
34378
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
3
14952
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10181
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 code... TCHAR myArray; DoStuff(myArray);
25
2946
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 construction and then.. // some other processing and/or changing 'retval' return retval; }
11
8131
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
6
5581
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 object collection and stanrd using it it starts to fall apart. Clearly there is something about javascript's usage of passing "By ref" that i am not getting. i have had a look on the web and found some examples, but i cant see why my code does not...
22
3501
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); //do I need to de reference here or just pass theString without the "ref"
8
3504
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
5934
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 them I just set their datasource. I am guessing this is probably what is causing the problem. Is there a better way to do this? Anyway this all works happily and things show up when the record already exists but I have 2 problems ; 1) When I add...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8978
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.