GCC:减少输出中的模板名称

GCC: cutting down template names in output

本文关键字:输出 GCC      更新时间:2023-10-16

在我的C++代码中,我经常使用模板。。这可能是一种轻描淡写的说法。最终的结果是,类型名称需要超过4096个字符,观看GCC输出至少是痛苦的。

在一些调试包(如GDB或Valgrind)中,可以请求不要对C++类型进行解映射。是否有类似的方法可以强制G++只输出损坏的类型名称,从而减少所有不必要的输出?

澄清

由于给出的第一个答案,我发现这个问题并不清楚。考虑以下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;
};

只有当类型为C<myType>的对象被实例化并且方法C::f()被调用时,行this->bar = T(2);中的错误才是错误。因此,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提供损坏的类型名而不是全名,或者以某种方式限制它们的长度?

STLFilt

不幸的是,STLFilt只会使输出更漂亮;长度不变。事实上,输出被分成多行的事实使整个情况变得更糟,因为输出占用了更多的空间。

长期以来,人们都在忍受C++错误报告的这一特殊缺陷。:)

但是,更详细的错误报告通常更适合解决复杂的问题。因此,一种更好的方法是让g++吐出长而详细的错误消息,然后使用独立的错误解析器来提高输出的可读性。

这里曾经有一个不错的错误分析器:http://www.bdsoft.com/tools/stlfilt.html(不幸的是,已不再开发)。

另请参阅此近乎重复的内容:解密C++模板错误消息

这永远不会奏效。当你制作模板类时:

B<A<int> >

这个类的定义中不再包含函数f()。如果你用clang++编译它,你会得到错误(其中测试类型为B<A<int> >):

error: no member named 'f' in 'B<A<int> >'
        test.f();
        ~~~~ ^

如果您想要更易读的错误,请尝试使用clang++。

您可以使用typedef:

typedef queue<int> IntQueue;

显然,在这个例子中,您只删除了类型名称的2个字符,但对于更复杂的例子,这将缩小更多的字符,并提高代码的可读性。

此外,这可能有助于IDE的自动完成功能(如果您使用的话)。