Re: [eigen] Pass POD by reference or by pointer?

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


On 16.02.2013 07:19, 何震邦 wrote:
Many C++ books say pass by reference whenever possible. However, I find the
code below misleading.

### unsupported/Eigen/src/MatrixFunctions/MatrixPower.h

  81 template<typename Derived, typename ResultType>
  82 void apply(const Derived&, ResultType&, bool&);

209 apply(b, res, init);

###

At line 209, it looks as if init would never be written. This is also why Qt
prefers pointers to references.

And how do you see from line 209 alone whether b or res are written to or from? (Assuming the variable name 'res' would not suggest it is an output variable) Maybe it's rather an argument to rename 'init' to sth like 'is_initialized' To see which variables are in-/output parameters you need to look at least at the signature anyways and if the function is neither documented, nor has a descriptive name you need to look into it anyways to see what it actually does (which is ok for functions that short).

When looking at a signature I'd rather say that passing pointers is misleading, because I would think that the function actually expects an array (as for example the Map constructors) rather than a single variable.

Actually, in that same file I'd suggest also changing modfAndInit that it takes a reference instead of a pointer and you might even consider passing RealScalar by const reference instead of by value (RealScalar is not guaranteed to be a POD).

Regards,
Christoph


I think there is nothing wrong to pass a reference to a non-POD, but passing a
reference to a POD is functionally safe but syntactically misleading. Even if
we want the functional safety of a reference, we can pass a pointer-const
instead, but the constness seems unnecessary.

If passing a pointer is better, feel free to alter the code. ;)

Thanks,
Chen-Pang



--
----------------------------------------------
Dipl.-Inf., Dipl.-Math. Christoph Hertzberg
Cartesium 0.049
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen

Tel: +49 (421) 218-64252
----------------------------------------------



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/