我有一個類,它具有將回傳Eigen::Matrix型別的方法。但是,當這些將是 1x1 矩陣時,我只想 return double,因為將標量視為Eigen::Matrix<double, 1, 1>有點討厭。本質上,我想要這樣的東西:
template <int rows, int cols>
class foo
{
using my_type = ((rows == 1 && cols == 1) ? double : Eigen::Matrix<double, rows, cols>);
};
但是,這(不出所料)不起作用。我怎樣才能創建上述的等價物?
uj5u.com熱心網友回復:
使用std::conditional_t<cond, TThen, TElse>:
template <int rows, int cols>
class foo
{
using my_type = std::conditional_t<rows == 1 && cols == 1, double, Eigen::Matrix<double, rows, cols>>;
};
如果你還沒有std::conditional_t<>,你可以試試std::conditional<>::type(帶typename前綴)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515139.html
標籤:C 模板
