我试图得到一个string报告一个类没有装饰在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”作为输出string? 现在我不能使用C ++ 11的方法。
更新令人遗憾的是,这个库在底层使用了相同的底层demangling API:请参阅@cv_and_he的评论
你可以尝试更新的TypeIndex库:
住在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
在我的编译器上