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

call by value / (const) reference

Hi,

I've several methods which get large objects as parameters like:

qr_decomposition(const DenseMatrix &A)

My question is simple: Should the line above be the preferred way or
should the parameter be given like

qr_decomposition(DoubleDenseMatrix A)

I know the difference in semantics. But does it make some essential
difference
for the compiler? Is it better to have the compiler do the copy or to do it
by oneself inside the method? Any performance differences?

regards,
alex
Jul 22 '05 #1
9 3969
Alexander Stippler wrote:
Hi,

I've several methods which get large objects as parameters like:

qr_decomposition(const DenseMatrix &A)

My question is simple: Should the line above be the preferred way or
should the parameter be given like

qr_decomposition(DoubleDenseMatrix A)

I know the difference in semantics. But does it make some essential
difference
for the compiler? Is it better to have the compiler do the copy or to do
it by oneself inside the method? Any performance differences?

regards,
alex


If you have to copy the object, it does not really matter. If you do not
need to copy it inside the function use the const-ref form.

--
WW aka Attila
:::
There are wo kinds of people: Those who finish what they start
Jul 22 '05 #2

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:40******@news.uni-ulm.de...
Hi,

I've several methods which get large objects as parameters like:

qr_decomposition(const DenseMatrix &A)

My question is simple: Should the line above be the preferred way or
should the parameter be given like

qr_decomposition(DoubleDenseMatrix A)

I know the difference in semantics. But does it make some essential
difference
for the compiler?
Typically yes, the generated code for the two forms will be different.
Is it better to have the compiler do the copy or to do it
by oneself inside the method? Any performance differences?


If the object is 'large', (as you state above), then imo pass
by reference should be preferred. But the only conclusive way
to find out about performance is to measure.

-Mike
Jul 22 '05 #3

"White Wolf" <wo***@freemail.hu> wrote in message
news:bv**********@phys-news1.kolumbus.fi...
Alexander Stippler wrote:
Hi,

I've several methods which get large objects as parameters like:

qr_decomposition(const DenseMatrix &A)

My question is simple: Should the line above be the preferred way or should the parameter be given like

qr_decomposition(DoubleDenseMatrix A)

I know the difference in semantics. But does it make some essential difference
for the compiler? Is it better to have the compiler do the copy or to do it by oneself inside the method? Any performance differences?

regards,
alex
If you have to copy the object, it does not really matter. If you

do not need to copy it inside the function use the const-ref form.


Andrei Alexandrescu (of "Modern C++ Design") has argued that passing
by const ref when you need to make a copy internally is less efficient
than passing by value. See
http://lists.boost.org/MailArchives/boost/msg10536.php.

Jonathan
Jul 22 '05 #4
In article <40******@news.uni-ulm.de>, Alexander Stippler wrote:
Hi,

I've several methods which get large objects as parameters like:

qr_decomposition(const DenseMatrix &A)

My question is simple: Should the line above be the preferred way or
should the parameter be given like

qr_decomposition(DoubleDenseMatrix A)

I know the difference in semantics. But does it make some essential
difference
for the compiler? Is it better to have the compiler do the copy or to do it
by oneself inside the method? Any performance differences?


I think it's better to use const reference, because otherwise, you are
"disclosing" the fact that you deep-copy in the interface, but this should
be an implementation detail. The client code should not know whether or not
you deep-copy.

It's actually not hard to see how one could "implement" this without a deep
copy. For example, you could further down the road use a third party library
to do it instead, in which case the "copying" would involve glue code that
converted your matrix into a data structure the other library could use.

The example may seem contrived or non-applicable, but it also illustrates that
one signature imposes more constraints on implementation than the other.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #5
Alexander Stippler wrote:
I've several methods which get large objects as parameters like:

QRDecomposition qr_decomposition(const DoubleDenseMatrix &A);

My question is simple:
Should the line above be the preferred way?
Or should the parameter be given like

QRDecomposition qr_decomposition(DoubleDenseMatrix A);

I know the difference in semantics.
But does it make some essential difference for the compiler?
These two definitions are equivalent
as far as the calling program is concerned.
Is it better to have the compiler do the copy
Or to do it by oneself inside the method?
It doesn't matter if you implementation calls for a copy to be made.
Any performance differences?


No.

Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library (SVMTL)

http://www.netwood.net/~edwin/svmtl/

(svmtl/src/matrix/Matrix.ccP: line #481-517)

Most implementations [in any computer programming language]
provide a method to perform the decomposition *in-place*:

DoubleDenseMatrix& qr_decomposition(DoubleDenseMatrix &A);

The QR decomposition is "packed" back into DoubleDenseMatrix A
in a format defined be the implementation
(and, in this case, a reference to A is returned).
This isn't a very good Application Program Interface (API)
because a DoubleDenseMatrix object has effectively
been converted into a QRDecomposition object
and is no longer a meaningful DoubleDenseMatrix object.
Obviously, this will certainly lead some unwary application programmer
to grief.

Jul 22 '05 #6
Donovan Rebbechi wrote:
I think it's better to use const reference because, otherwise,
you are "disclosing" the fact that you deep-copy in the interface
but this should be an implementation detail.
The client code should not know whether or not you deep-copy.
Please show us an example of a "client" program
that can tell whether the function was declared

int f(int);

or

int f(const int&);
It's actually not hard to see how one could "implement" this
without a deep copy. For example, you could, further down the road,
use a third party library to do it instead,
in which case the "copying" would involve glue code that converted
your matrix into a data structure the other library could use.

The example may seem contrived or non-applicable
but it also illustrates that one signature imposes more constraints
on implementation than the other.


You can simply substitute an f(const int&) declaration
for the f(int) declaration in your header file.
The only "constraint" is that
you would need to recompile the application.

Jul 22 '05 #7
In article <40************@jpl.nasa.gov>, E. Robert Tisdale wrote:
Donovan Rebbechi wrote:
Please show us an example of a "client" program
that can tell whether the function was declared

int f(int);

or

int f(const int&); [snip] The only "constraint" is that
you would need to recompile the application.


You're right, but some people actually think binary compatibility is important.

Even if this is only a small advantage, the pass-by-const reference doesn't
have any disadvantages (or did I miss something)

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #8

"Donovan Rebbechi" <ab***@aol.com> wrote in message:

Even if this is only a small advantage, the pass-by-const reference doesn't have any disadvantages (or did I miss something)


Supposedly, it may inhibit some optimizations. See
http://lists.boost.org/MailArchives/boost/msg10536.php.

Jonathan
Jul 22 '05 #9
Donovan Rebbechi wrote:
Even if this is only a small advantage,
the pass-by-const reference doesn't have any disadvantages
(or did I miss something)


int f(const int&);

It's probably best to pass small objects by value
instead of by const reference.

Jul 22 '05 #10

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

Similar topics

4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
2
by: Lateralus | last post by:
headers/vector3.h: In member function ‘Vector3 Vector3::operator*(Scalar)’: headers/vector3.h:13: error: no matching function for call to ‘Vector3::Vector3(Vector3)’...
5
by: homsan toft | last post by:
Hi, I'm (still) trying to return a pair<const Key, T> from iterator dereference. So I defined a proxy class in the obvious way: template<class KeyT, class DataT> struct ref_proxy { typedef...
8
by: priyasmita_guha | last post by:
C uses call by value for passing of parameters in contrast to C++ which uses call by reference.How is call by value advantageous and how is it implemented?
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 {
3
by: uday.sen | last post by:
Hi, I am porting a piece of code from VC++ to linux platform. My compiler is g++ 3.2.2. I am getting following error: no matching function for call to A::setResponse(std::wstring) candidates...
8
by: Michael Safyan | last post by:
Dear members of comp.lang.c++, I am a little bit confused about the differences between constant references and values. I understand that it is faster to use a constant reference ("const T&") than...
11
by: Frederick Gotham | last post by:
We all know that there's special rules for the binding of a "reference to const" to an R-value, i.e.: int const &i = 5; acts as if it were: int const __rval = 5; int const &i = __rval;
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
2
by: cablepuff | last post by:
template <typename ContainerType> ContainerType rsa_encrypt_list(const std::string&, const typename ContainerType::reference, const typename ContainerType::reference); const BigInteger...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...
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...

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.