首页 > 解决方案 > 如何转换为模板类型?

问题描述

在 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.

标签: gdb

解决方案


您可以做到这一点的一种方法是使用类型的错位名称。例如,std::vector<int>当前 gcc 和 libstdc++ 的错误名称是_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

推荐阅读