Registered Member
|
Hello,
I have a function that is only valid vor Vector2d, but it is also called with VectorXd of size 2:
Calling f2d with b works and with a fails, of course. Calling fXd works with any vector, but also with Vector3d. Is there a way to formulate a function signature that only accepts VectorXd of size 2 and Vector2d? Or is impossible, because the size of a VectorXd is a runtime property and I should just use runtime assertions to make sure it's only called with a vector of size 2? Thanks! |
Moderator
|
You can use Ref<>:
|
Registered Member
|
Thanks, that works like that:
Is it possible to add an overload that is called by VectorXd of size 3? Just adding a function that takes an const Ref<Vector3d> & seems to be ambigous to the compiler. We are converting our application from a self-written LA lib to Eigen and my actual problem looks like that:
This worked on our previous lib, because there were no compile time sized vectors. My idea is to split this function into two functions. One that takes an Vector2d and VectorXd of size=2, one that takes an Vector3d and VectorXd of size=3. Ok, my bad... it seems to be working with the easy solution of just converting to Vector3d resp. Vector2d and taking VectorXd as arguments. Still, out of interest, do you see a way to formulate the overloads, how I said above? Also the performance impact on the runtime conversion bothers me. Thx! |
Moderator
|
You need 3 overloads, one taking a Ref<Vector2d>, one taking a Ref<Vector3d>, and one for VectorXd doing the branching at runtime:
if(a.size()==2) foo(a.head<2>(); else if(a.size()==3) foo(a.head<3>(); with perhaps the help of std::enable_if to disambiguate. |
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]