Improved pretty printing for smart pointers

Jonathan Wakely jwakely.gcc@gmail.com
Wed Apr 24 07:48:00 GMT 2013


On 24 April 2013 07:08, Michael Marte wrote:
> Hello *,
>
> I found the pretty printers for std::shared_ptr, std::weak_ptr and
> std::unique_ptr (as provided by
> svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python) not sufficient for
> daily use as they only generate a description (including the address of
> the pointee) but do not pretty print the pointee itself. This behaviour
> implies that one has to copy the address of the pointee, cast it to the
> pointee's type, and dereference the result.

Or you just say 'print *ptr' and GDB dereferences it for you, which is
the same as a built-in pointer.

This works for shared_ptr but not currently for unique_ptr, I'm not
sure why, I'll look into that (I also want to improve the output for a
unique_ptr with custom deleter)

For weak_ptr, which has no dereference operator to use but you can say
'print *wptr._M_ptr' (and that works for shared_ptr too)


> To improve on the current
> state of affairs, I improved the pretty printers to generate children.
> For std::shared_ptr and std::weak_ptr, three children are generated,
> namely "Use count", "Weak count", and "Managed value". For
> std::unique_ptr, one child is generated, namely "Managed value". Using
> this solution with an IDE like kdevelop or Eclipse CTD, it only needs
> two clicks to see the value managed by a smart pointer.

I haven't checked what the output is, but I'm unsure about making this
change. I like having the smart pointers behave basically like a
built-in pointer.  If you want the value you can dereference the
pointer.

> I attached my patch in the hope that you will find it interesting and
> that it will be integrated into the suite of GCC STL pretty printers.

Do you have a GCC copyright assignment submitted to the FSF?  That is
required before significant patches can be accepted. This might be
small enough to not require it though.



More information about the Libstdc++ mailing list