Boost logo

Boost Users :

From: Ivan Vecerina (yg-boost-users_at_[hidden])
Date: 2003-02-13 09:41:45


<speedsnaii_at_[hidden]> wrote in message news:b2g7nj+6gee_at_eGroups.com...
| A question on use of the matrix and vector classes:
|
| Should I:
...
| matrix<double> foo(const matrix<double> a)
...
| void foo(matrix<double>& r, const matrix<double> a)
...
| In other words: is it expensive to construct the temporary in case
| one? As I understand, the class would need an underlying 'copy on
| write' to be cheap, similar as strings do.

By default, the copy constructor of boost::numeric::ublas::matrix
appears to copy its representation in a newly allocated block.
As far as I can tell, this behavior could be customized by
using a custom storage type (the third template parameter).

However, many optimizing compilers are able to eliminate
unnecessary copying of the return value of a function.
(This is called RVO or NRVO for Return Value Optimization and
 Named..., depending on how the function is written).
For example, MSVC suppresses the unnecessary copy,
even in Debug mode with no optimizations enabled.

So I think you should prefer the interface which is more
natural to use:
  matrix<double> foo(const matrix<double> a)

I hope this helps.

--
 Ivan Vecerina, Dr. med.  <>  http://www.post1.com/~ivec
 Soft Dev Manger, xitact  <>  http://www.xitact.com
 Brainbench MVP for C++   <>  http://www.brainbench.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net