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?
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?
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.
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.
" 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.
There is no difference between using <typename T> OR <class T> ; i.e. it is a convention used by C++ programmers.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With