C++ Logo

sg16

Advanced search

Re: [isocpp-lib-ext] LEWG(I) (Bi)Weekly review - P2510R0: Formatting pointers

From: Mark de Wever <koraq_at_[hidden]>
Date: Sat, 12 Feb 2022 19:03:44 +0100
On Fri, Feb 11, 2022 at 02:12:01AM +0000, Zhihao Yuan wrote:
> On Thursday, February 10th, 2022 at 3:40 PM, Inbal Levi via Lib-Ext <lib-ext_at_[hidden]> wrote:
>
> > P2510R0: Formatting pointers
> > From the Abstract:
> > [...] This paper aims to make formatting pointer types more useful, reducing the need for users to write their own formatters or casting a pointer type to an integer type.
>
> The argument "pointer types should
> share the same amount of formatting
> options as as integers" is undefended.
>
> To explain what do I mean -- I have
> a stronger need for seeing
>
> std::println("at {:?p}", p); // '?' is introduced in http://wg21.link/p2286.

Note the ? in https://wg21.link/p2286R6 is a type specifier and not a
new element of the std-format-spec. So the example should be

   std::println("at {:?}", p);

> output:
>
> at 00007ffb`a67fb331 myLib!myNs::SomeCls
>
> Address is an identity of object in C++,
> so when printing an address, I want to
> see what is there. I hope we can build
> the understanding of pointers != integers
> after http://wg21.link/p2318 (provenance-
> aware memory object model).

I wasn't aware of p2318, but I'm aware of the concept of pointer
provenance. I had a look at the paper, but didn't find an example how
printf's output for %p changes with this proposal. Do you have an
example?

The format specializations affected by this paper only format the types
(const) void* and nullptr_t. Effectively only the formatter of
const void* is used since that's how the pointer is stored in a
basic_format_arg object. So the pointer has no information regarding the
object type it points at, if any.

For formatting pointers of your own types you need to provide your own
formatter specialization. There you can implement printing the type of
the pointer if wanted.

I think your example can certainly be useful, but it's outside the scope
of this proposal.

Cheers,
Mark

Received on 2022-02-12 18:03:48