我正在嘗試將自定義轉換運算符寫入Eigen::Matrix2d
對象,並且我慘敗了。下面是剝離到的骨代碼:Eigen中的轉換運算符
#include <iostream>
#include <Eigen/Dense>
struct MatrixView
{
operator Eigen::Matrix2d() const // conversion operator
{
Eigen::Matrix2d tmp;
std::cout << "CONVERSION TRIGERRED\n";
return tmp;
}
};
int main()
{
MatrixView m;
static_cast<Eigen::Matrix2d>(m);
}
我得到一個討厭的編譯時錯誤,太長的方式在這裏列出,首先是:
error: no matching function for call to 'Eigen::Matrix::_init1(const MatrixView&)'
note: cannot convert 'x' (type 'const MatrixView') to type 'Eigen::Index {aka long int}' Base::template _init1(x); Base::template _init1(x);
您可以找到完整的錯誤訊息here。
我不知道發生了什麼事情,轉換運算符是微不足道的,它只是返回默認初始化的Eigen::Matrix2d
。任何想法有什麼問題嗎?
編輯
如果我刪除 「明確」 則轉換是通過複製初始化觸發,像
Eigen::Matrix2d tmp = m; // OK without "explicit"
然而static_cast
仍然失敗。
平臺細節:
OS X 10.10約塞米蒂,本徵3.2.6,克++(MacPorts的gcc5 5.2.0_0)5.2.0,蘋果LLVM版本7.0.0(鐺-700.0.72)靶: x86_64-apple-darwin14.5.0,都無法編譯代碼。
EDIT 2
整個事件實際發生的,因爲我的鏈接是指向Eigen_3.3_alpha1的開發者版本。在Eigen 3.2.x中它可以工作。感謝@Matt的提示!我會結束這個問題。
該注意事項使您的操作符爲'const'。值得一試。 –
@ RollenD'Souza仍然沒有區別......我實際編輯了代碼並添加了'const',所以現在很清楚我沒有違反'const''。 – vsoftco
不是'Matrix2d'只是'Matrix的模板。你有沒有嘗試過使用底層的'Matrix'本身? –
Matt