如何转换为模板类型?

时间:2019-06-05 20:32:36

标签: gdb

在gdb中,如果您有指向某物的指针,则可以在打印它之前对其进行转换。

例如,这有效:

print *(int*) 0xDEADBEEF

但是,如何打印std::vector<T>?具体是std::vector<std::string>

如果它是std::string,我可以用std::__cxx11::string输出的whatis std::string来做,但是我不能说服gdb喜欢std::vector<int>(作为示例)。如它所说,No symbol "std::vector<int>" in current context.

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是使用类型的错误名称。例如,当前gcc和libstdc ++上std::vector<int>的错误名称是_ZSt6vectorIiSaIiEE,我是通过在Compiler Explorer上编译以下代码找到的:

#include <vector>

void foo(std::vector<int>) {}
// Mangled symbol name: _Z3fooSt6vectorIiSaIiEE
// _Z means "this is C++".
// 3foo means "identifier 3 chars long, which is `foo`"
// Strip those off and you're left with: St6vectorIiSaIiEE
// Add the _Z back: _ZSt6vectorIiSaIiEE

std::vector<std::string>的错误名称是:_ZSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE,可以用whatis进行验证。

实际执行演员表:

print *(_Z3fooSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE*) 0xDEADBEEF
相关问题