如何转换为模板类型? [英] How do I cast to a templated type?
问题描述
在gdb中,如果您有指向某物的指针,则可以在打印它之前对其进行转换.
In gdb, if you have a pointer to something, you can cast it before printing it.
例如,这有效:
print *(int*) 0xDEADBEEF
但是,如何打印std::vector<T>
?具体是std::vector<std::string>
?
However, how do I print a std::vector<T>
? Specifically a std::vector<std::string>
?
如果它是std::string
,我可以使用std::__cxx11::string
(它是whatis std::string
输出)来实现,但是我不能说服gdb喜欢std::vector<int>
(作为示例).引用它无济于事,就像它说的那样,No symbol "std::vector<int>" in current context.
If it's std::string
, I can do it with std::__cxx11::string
, which whatis std::string
outputs, but I can't convince gdb to like std::vector<int>
(as an example). Quoting it doesn't help, as it says, No symbol "std::vector<int>" in current context.
推荐答案
一种做到这一点的方法是使用类型混乱的名称.例如,当前gcc和libstdc ++上std::vector<int>
的错误名称是_ZSt6vectorIiSaIiEE
,我通过在
One way you can do this is by using the mangled name of the type. For example, the mangled name of std::vector<int>
on current gcc and libstdc++ is _ZSt6vectorIiSaIiEE
, which I found by compiling the following code on the 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
进行验证.
The mangled name of std::vector<std::string>
is: _ZSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE
, which can be verified with whatis
.
实际执行演员表:
print *(_Z3fooSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE*) 0xDEADBEEF
这篇关于如何转换为模板类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!