Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: cutting down template names in output

Tags:

c++

gcc

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.

like image 597
Up-and-coming LaTeX Mastah Avatar asked Dec 02 '13 00:12

Up-and-coming LaTeX Mastah


People also ask

How will you restrict the template for a specific datatype?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.

What is typename in template?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.

What is the difference between typename and class in template?

There is no difference between using <typename T> OR <class T> ; i.e. it is a convention used by C++ programmers.

Why are templates defined in header files?

To have all the information available, current compilers tend to require that a template must be fully defined whenever it is used. That includes all of its member functions and all template functions called from those. Consequently, template writers tend to place template definition in header files.


2 Answers

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

like image 121
oakad Avatar answered Sep 27 '22 20:09

oakad


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.

like image 40
kingtorus Avatar answered Sep 27 '22 18:09

kingtorus