7

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.

  • 4
    +1 for using templates a lot. – Kerrek SB Dec 02 '13 at 00:36
  • Just to clarify, you'd like the error message to name the type `_ZN9wikipedia7article8wikilinkC1ERKSs` instead of `wikipedia::article::wikilink::wikilink(std::string const&)`? If not, can you show what you mean by "mangled type names"? (I.e., what do you want the output to look like?) – chwarr Dec 02 '13 at 06:15
  • @c45207: could be that. Anything that is shorter than what I get. For example, it would be cool if the recursion stopped after the first level and show std::vector> instead of something like std::vector>. – Up-and-coming LaTeX Mastah Dec 13 '13 at 01:45

3 Answers3

2

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

Community
  • 1
  • 1
oakad
  • 6,945
  • 1
  • 22
  • 31
  • Any suggestions how to run STLFilt together with gcc? I can't wrap my head around it and its webpage isn't helping :). – Up-and-coming LaTeX Mastah Dec 02 '13 at 04:42
  • There's a script called "gfilt" included in the tarball. Just use it in place of g++: ./gfilt -c program.cpp – oakad Dec 02 '13 at 04:52
  • Unfortunately, STLFilt still produces very long type names, just now they are broken into things that are more readable. The thing is, I don't want them at all: I only need file and line numbers most of the time. I haven't found command line options to limit the length of a type name, is there one? – Up-and-coming LaTeX Mastah Dec 02 '13 at 05:12
  • What prevents you from editing the perl script in STLFIlt? Its rather simple. – oakad Dec 02 '13 at 06:11
  • Knowing perl and the inner workings of STLFilt, but I will get to it as soon as it becomes impossible to work with.. – Up-and-coming LaTeX Mastah Dec 13 '13 at 01:43
1

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.

kingtorus
  • 953
  • 12
  • 15
0

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