Re: [eigen] is returning Eigen::Ref objects legit? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] is returning Eigen::Ref objects legit?
- From: Sébastien Barthélémy <barthelemy@xxxxxxxxx>
- Date: Mon, 20 May 2013 19:06:39 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type; bh=5Fgu3uc4QXDAQZLDQrQz3XAPDan8K6Dropp61zW1/W0=; b=gJaXydCkMCBTKpVMjDPF1+nePTuRSK2lnd921YDmfqqFrvCBuzbeNGW7kmqv9knCQp VpI4Ul4lqmGQ+q6DGChbG1yRrtzRYilVk9STEMmwiHPb+k+33xS1K6CtTd81CDD397sT 4kxjf/MMQdz7PzU0W9jGYOh+M4vWe1/IT/TFRT2QqvMx+iIBVqiUCvY8/w2jIAkxjpcn YN1y2zL6a8HabYbwujIHb3r6KmkxxTKRl3mvMGSUOCzgeyyIT9P2wjAokEq/o7j0v1G1 waI7oLfoMhvrYcjwE9mwImxJkxNL4Enw1CnTbmqQgYNb37LgOSAqA1mUWZLtXN/vQxhQ xPKA==
On Mon, May 20, 2013 at 6:36 PM, 何震邦 <jdh8@xxxxxxxxxxxxxx> wrote:
> 在 2013 五月 20 週一 15:38:12,Sébastien Barthélémy 寫道:
>> Hello,
>>
>> I'm giving new Eigen::Refs a try and thought they might be a handy
>> type to return (by const ref), especially when the implementation
>> knows the matrix size at compile time but the caller does not.
>
> Yes, it's a wrapper of reference.
ok, got it, I should have returned the Ref object by value. It seems I
expect Eigen to be a bit too magical.
Thank you for your help!
PS: Fixed example attached.
#include <iostream>
#include <Eigen/Eigen>
#include <memory>
class Interface {
public:
virtual size_t size() const = 0;
virtual void compute() = 0;
virtual Eigen::Ref<const Eigen::Matrix<float, 6, Eigen::Dynamic>, Eigen::Aligned>
get() const = 0;
virtual ~Interface() {};
};
class Implementation10 : public Interface {
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Implementation10() {};
size_t size() const {
return 10;
}
void compute() {
m_ = m_.Identity();
}
Eigen::Ref<const Eigen::Matrix<float, 6, Eigen::Dynamic>, Eigen::Aligned>
get() const {
return m_; // warning: returning reference to temporary
}
private:
Eigen::Matrix<float, 6, 10> m_;
};
Interface* createInterface() {
return new Implementation10();
}
int main()
{
std::cout << "Hello, world" << std::endl;
const std::auto_ptr<Interface> interface(createInterface());
Eigen::Matrix<float, 3, Eigen::Dynamic> triple(3, interface->size());
interface->compute();
triple = 3 * interface->get().topRows(3);
std::cout << triple;
return 0;
}