2

我正在尝试获取一个字符串,该字符串在 Windows(Visual Studio 2010)上报告一个没有装饰的类类型,但根本没有成功。

由于 UnDecorateName 不起作用,我正在使用 boost 库。

#include <typeinfo>
#include <boost/core/demangle.hpp>

class MyObject{};

int main (int argc,  char ** argv)
{
    MyObject o;
    const char * str = typeid(o).name(); // str = "class MyObject"
    std::string dstr = boost::core::demangle( str ); // dstr = "class MyObject"

    return 0;
}

如何从上面的代码中仅获取“MyObject”作为输出字符串?现在我不能使用 c++11 方法。

4

1 回答 1

1

更新遗憾的是,这个库在底层使用了相同的底层拆解 API:参见@cv_and_he 的评论

您可以尝试更新的 TypeIndex 库:

Live On Coliru

#include <boost/type_index.hpp>
#include <iostream>

class MyObject { public: virtual ~MyObject() {} };
struct Derived : MyObject {};

int main() {
    MyObject o;
    Derived d;

    std::cout << boost::typeindex::type_id<MyObject>().pretty_name() << "\n";
    std::cout << boost::typeindex::type_id<Derived>().pretty_name() << "\n";

    MyObject& r = d;
    std::cout << boost::typeindex::type_id_runtime(r).pretty_name() << "\n";
}

印刷

MyObject
Derived
Derived

在我的编译器上

于 2015-10-28T09:48:27.727 回答