473,389 Members | 1,366 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,389 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 2981
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.