GCC: cutting down template names in output
在我的C代码中,我经常使用模板。最终结果是类型名称占用了4096个以上的字符,并且至少可以说看着GCC输出很痛苦。
在某些调试程序包(如GDB或Valgrind)中,可以要求不对C类型进行解密。有没有一种类似的方法可以强制G仅输出错误的类型名称,从而削减所有不必要的输出?
澄清度
由于给出了第一个答案,因此我认为问题不清楚。请考虑以下MWE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | test.cpp: In instantiation of a€?void C< T >::f() [with T = B<B<B<B<A<int> > > > >]a€?: test.cpp:33:8: required from here test.cpp:21:14: error: no matching function for call to a€?B<B<B<B<A<int> > > > >::B(int)a€? 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 a€?inta€? to a€?const B<B<B<B<A<int> > > > >&a€? test.cpp:21:14: error: a€?class C<B<B<B<B<A<int> > > > > >a€? has no member named a€?fooa€? this->foo = T(1); ^ test.cpp:23:14: error: no matching function for call to a€?B<B<B<B<A<int> > > > >::B(int)a€? 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 a€?inta€? to a€?const B<B<B<B<A<int> > > > >&a€? test.cpp:23:14: error: a€?class C<B<B<B<B<A<int> > > > > >a€? has no member named a€?bara€? this->bar = T(2); |
这里的类型名称令人讨厌,但是当完整的类型名称需要数百个字符时,将无法读取。有什么方法可以要求GCC输入整齐的类型名称而不是全名,或以某种方式限制它们的长度?
STL筛选
不幸的是,
随着年龄的增长,人们正遭受C错误报告这一特殊缺陷的困扰。 :)
但是,对于复杂的问题解决,一般来说,详细的错误报告越多越好。因此,更好的方法是让g吐出冗长而冗长的错误消息,然后使用独立的错误解析器使输出更具可读性。
这里曾经有一个不错的错误解析器:http://www.bdsoft.com/tools/stlfilt.html(不幸的是,不再开发中)。
另请参见以下重复内容:解密C模板错误消息
这将永远无法工作。当您创建模板类时:
1 | B<A<int> > |
此类在其定义内不再具有函数f()。如果使用clang进行编译,则会收到错误消息(其中test的类型为
1 2 3 | error: no member named 'f' in 'B<A<int> >' test.f(); ~~~~ ^ |
如果您想要更多可读性更高的错误,请尝试使用clang。
您可以使用
1 | typedef queue<int> IntQueue; |
很显然,在此示例中,您仅删除了类型名称的2个字符,但是对于更复杂的示例,这将缩小更多字符的范围,并提高代码的可读性。
此外,这可能有助于IDE的自动完成功能(如果使用的话)。