I have tried writing a function which takes a ColXpr
value as input:
typedef Eigen::Array Signal2D;
vo
In C++, a temporary object cannot bind to a non-const reference.
I'm assuming that arr.col(0)
returns an object by value. The returned value is a temporary object. This means it cannot match a parameter of type T &
.
One solution:
auto temp = arr.col(0);
Threshold(temp);
I wonder if you intended for arr.col()
to return a reference?
To add to the answer above:
Eigen return expressions are temporaries. E.g. ColXpr
is just a small copyable object that gives you access to the array data. It is returned by value, ColXpr col(...)
. You can capture the ColXpr temporary with const reference but not otherwise.
However you can write:
void Threshold(Matrix::ColXpr col);
Generally, it is frowned upon to modify Eigen expression in functions. The preferred way is to write unary/binary functors/lambdas eg:
array.col(i) = array.col(i).unaryExpr(
[](const float &value) {
return float(value >= 0);
}
);
Btw, if you intend to write generic Eigen functions, use Eigen::EigenBase<B>
as arguments to capture any eigen expression, see http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html
Boost serialization requires non-temporary reference as well, even the operand is being written to an archive.
To complete previous answers, you're probably looking for the Ref<>
class which will allow you to write a function that accept both columns of a matrix and vectors without templates nor copies:
void threshold(Ref<ArrayXf> params) {
params = (params >= 0 ).cast<float>();
}
ArrayXf a;
ArrayXXf A;
/* ... */
threshold(a);
threshold(A.col(j));