【发布时间】:2013-12-02 00:35:17
【问题描述】:
在我的 C++ 代码中,我经常使用模板。这可能是轻描淡写。最终结果是类型名称包含超过 4096 个字符,至少可以说看着 GCC 输出很痛苦。
在 GDB 或 Valgrind 等几个调试包中,可以要求不要对 C++ 类型进行解组。是否有类似的方法强制 G++ 输出only 错位的类型名称,切断所有不必要的输出?
澄清
由于给出的第一个答案,我发现问题并不清楚。考虑以下 MWE:
template <typename T>
class A
{
public:
T foo;
};
template <typename T>
class B
{
};
template <typename T>
class C
{
public:
void f(void)
{
this->foo = T(1);
this->bar = T(2);
}
};
typedef C< B< B< B< B< A<int> > > > > > myType;
int main(int argc, char** argv)
{
myType err;
err.f();
return 0;
};
this->bar = T(2); 行中的错误仅在实例化C<myType> 类型的对象并调用C::f() 方法时才会出现错误。因此,G++ 返回如下错误消息:
test.cpp: In instantiation of ‘void C<T>::f() [with T = B<B<B<B<A<int> > > > >]’:
test.cpp:33:8: required from here
test.cpp:21:14: error: no matching function for call to ‘B<B<B<B<A<int> > > > >::B(int)’
this->foo = T(1);
^
test.cpp:21:14: note: candidates are:
test.cpp:11:7: note: B<B<B<B<A<int> > > > >::B()
class B
^
test.cpp:11:7: note: candidate expects 0 arguments, 1 provided
test.cpp:11:7: note: B<B<B<B<A<int> > > > >::B(const B<B<B<B<A<int> > > > >&)
test.cpp:11:7: note: no known conversion for argument 1 from ‘int’ to ‘const B<B<B<B<A<int> > > > >&’
test.cpp:21:14: error: ‘class C<B<B<B<B<A<int> > > > > >’ has no member named ‘foo’
this->foo = T(1);
^
test.cpp:23:14: error: no matching function for call to ‘B<B<B<B<A<int> > > > >::B(int)’
this->bar = T(2);
^
test.cpp:23:14: note: candidates are:
test.cpp:11:7: note: B<B<B<B<A<int> > > > >::B()
class B
^
test.cpp:11:7: note: candidate expects 0 arguments, 1 provided
test.cpp:11:7: note: B<B<B<B<A<int> > > > >::B(const B<B<B<B<A<int> > > > >&)
test.cpp:11:7: note: no known conversion for argument 1 from ‘int’ to ‘const B<B<B<B<A<int> > > > >&’
test.cpp:23:14: error: ‘class C<B<B<B<B<A<int> > > > > >’ has no member named ‘bar’
this->bar = T(2);
这里的类型名称很烦人,但是当完整的类型名称需要数百个字符时,就无法阅读。有没有办法向 GCC 询问损坏的类型名称而不是全名,或者以某种方式限制它们的长度?
STL过滤
不幸的是,STLFilt 只会让输出更漂亮;长度不变。事实上,输出被分成多行的事实让整个事情变得更糟,因为输出占用了更多的空间。
【问题讨论】:
-
+1 大量使用模板。
-
澄清一下,您希望错误消息将类型命名为
_ZN9wikipedia7article8wikilinkC1ERKSs而不是wikipedia::article::wikilink::wikilink(std::string const&)?如果没有,您能否说明“损坏的类型名称”是什么意思? (即,您希望输出是什么样的?) -
@c45207:可能是这样。任何比我得到的更短的东西。例如,如果递归在第一级之后停止并显示 std::vector<:vector>> 而不是 std::vector<:vector>> 之类的东西会很酷。