GCC: cutting down template names in output

In my C++ code I use templates a lot.. that might be an understatement. The end result is that the type names take more than 4096 characters and watching the GCC output is painful to say the least.

In several debugging packages like GDB or Valgrind one can request that C++ types not be demangled. Is there a similar way to force G++ to output only mangled type names, cutting on all the unecessary output?

Clarification

Because of the first answer that was given I see that the question isn't clear. Consider the following 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;
};

The error in the line this->bar = T(2); is an error only when an object of type C<myType> is instantiated and the method C::f() called. Therefore, G++ returns an error message along these lines:

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);

The type names are irritating here, but make it impossible to read when the complete type name takes hundreds of characters. Is there a way to ask GCC for mangled type names instead of the full names, or to limit their length somehow?

STLFilt

Unfortunately, STLFilt only makes the output prettier; the length doesn't change. In fact, the fact that the output is broken into multiple lines makes the whole thing worse, because the output takes more space.


People are suffering from this particular shortcoming of C++ error reporting for ages. :)

However, more verbose error reports are generally better for complex problem solving. Thus, a better approach is to let g++ to spit out the long and verbose error message and then use a stand alone error parser to make the output more readable.

There used to be a decent error parser here: http://www.bdsoft.com/tools/stlfilt.html (unfortunately, no longer in development).

See also this near duplicate: Deciphering C++ template error messages


This will never work. When you make the template class:

B<A<int> >

this class no longer has the function f() within its definition. If you compile this with clang++ you'll get the error (where test is of type B<A<int> > ):

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

Try using clang++ if you want slightly more readable errors.


You could use typedef :

typedef queue<int> IntQueue;

Obviously, at this example you removed only 2 characters of the type name, but with more complex examples this will narrow down even more chars and will improve readability of your code.

Also, this may help the auto-completing feature of your IDE (if you use one).

链接地址: http://www.djcxy.com/p/17538.html

上一篇: 如何嵌入在树枝中的PHP

下一篇: GCC:在输出中减少模板名称