libstdc++ Chapter 28. Demangling example

Jonathan Wakely jwakely@redhat.com
Tue Apr 18 14:49:58 GMT 2023


On Tue, 18 Apr 2023 at 14:00, Jonny Grant <jg@jguk.org> wrote:
>
>
>
> On 12/04/2023 17:07, Jonny Grant wrote:
> >
> >
> > On 12/04/2023 16:36, Jonathan Wakely wrote:
> >> On Wed, 12 Apr 2023 at 16:10, Jonny Grant <jg@jguk.org> wrote:
> >>>
> >>>
> >>>
> >>> On 04/04/2023 12:26, Jonathan Wakely wrote:
> >>>> On Fri, 31 Mar 2023 at 21:38, Jonny Grant wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>> On 31/03/2023 17:18, Jonathan Wakely wrote:
> >>>>>> On Fri, 31 Mar 2023 at 16:24, Jonny Grant <jg@jguk.org> wrote:
> >>>>>>>
> >>>>>>> Hello
> >>>>>>>
> >>>>>>> I tried out this code. Maybe I'm doing something wrong?
> >>>>>>>
> >>>>>>> https://godbolt.org/z/Y78h4f1W6
> >>>>>>>
> >>>>>>> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/libstdc++/manual/manual/ext_demangling.html
> >>>>>>>
> >>>>>>> However only get reduced output after status -2 is returned:
> >>>>>>>
> >>>>>>> std::bad_exception      =>
> >>>>>>>
> >>>>>>> Looks like std::cout gets broken by the nullptr realname (that's a frustration). So I put an if(realname) in the godbolt link above.
> >>>>>>
> >>>>>> N.B. passing a null pointer to printf("%s", ptr) is undefined.
> >>>>>> Printing "(null)" is not required by the standards.
> >>>>>>
> >>>>>> Anyway, the exception classes haven't printed their mangled name for
> >>>>>> many many years, if they ever did. Only the second part of the
> >>>>>> example, using typeid, is valid.
> >>>>>
> >>>>> Ok I see. Is it better that the std::bad_exception part of the example is removed?
> >>>>
> >>>> Yes, done for GCC 13.
> >>>
> >>> Great!
> >>>
> >>>>>
> >>>>> c++filt doesn't demangle St13bad_exception either.
> >>>>
> >>>> c++filt needs the _Z prefix that identifies a mangled C++ name. The
> >>>> __cxa_demangle function doesn't need that.
> >>>>
> >>>> $ c++filt St13bad_exception
> >>>> St13bad_exception
> >>>> $ c++filt _ZSt13bad_exception
> >>>> std::bad_exception
> >>>
> >>> Ok that makes sense.
> >>>
> >>>>>>> Maybe it's also a good time to update the example abi::__cxa_demangle call from those 0 parameters to be NULL?
> >>>>>>
> >>>>>> Maybe nullptr.
> >>>>>
> >>>>> ok
> >>>>
> >>>> I didn't change them, using 0 as a null pointer constant seems OK.
> >>>
> >>> Isn't nullptr, or at least NULL better? NULL has always been used in C++ code, I've seldom seen 0 used professionally.
> >>
> >> NULL is a macro, that requires inclusion of a header. nullptr is
> >> superior, but not available in C++98. 0 works OK here.
> >
> > Fair enough. One of those headers must have included <cstddef> - I recall it did compile with NULL.
> >
>
> https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es47-use-nullptr-rather-than-0-or-null
> ES.47: Use nullptr rather than 0 or NULL
>
> Where there's a 0 in an example, it gets copy-pasted. If the example use nullptr, that's what is copy-pasted.

Yes, exactly, and then it doesn't compile with -std=c++98



More information about the Libstdc++ mailing list