如何知道Eigen C++ Library中PlainObjectBase的类型?

问题描述 投票:0回答:1

我想知道函数内 Eigen::PlainObjectBase 变量的基本数据类型,最好是字符串。考虑以下示例:

template<class Derived>
void myFunction(const Eigen::PlainObjectBase<Derived>& matrix)
{
  // std::string basicDataType = ??? //basic dataType of Derived

}

Eigen::MatrixXd A;
myFunction(A); // should  print "double"

Eigen::VectorXf b;
myFunction(b); // should  print "float"

Eigen::Matrix<std::complex<float> > C;
myFucntion(C); // should  print "float". It would be also nice if I could know that it is "complex" and "float" :)
c++ templates eigen
1个回答
0
投票

Derived
有一个 typedef
Scalar
,它是对象的基础数据类型。 此代码:

#include <Eigen/Eigen>
#include <iostream>

template<class Derived>
void print_name(const Eigen::PlainObjectBase<Derived>&)
{
    std::cout << typeid(typename Derived::Scalar).name() << "\n";
}

int main() {
    print_name(Eigen::MatrixXd{});
    print_name(Eigen::MatrixXf{});
    print_name(Eigen::MatrixXi{});
}

输出

d
f
i

在我的带有 clang 18.1.0 的 Linux 机器上。请参阅现场演示。如果你想要一个更好的字符串名称,你可以实现一个重载函数,它为你的类型提供一个更好的字符串版本,或者阅读this

© www.soinside.com 2019 - 2024. All rights reserved.