C++ Logo


Advanced search

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

From: Jens Maurer <Jens.Maurer_at_[hidden]>
Date: Mon, 14 Feb 2022 21:37:26 +0100
On 14/02/2022 21.20, Mark de Wever via SG16 wrote:
> Implementing '?' on pointers seems non trivial and I'm not even sure
> whether it can be done without compiler support. Even when getting
> compiler support I directly see some issues.
> What type to print when a library, not part of your application, returns
> a 'void *'? The type is probably a type private to the library and it's
> not possible know the real type it's pointing at.

So what? The type is "void*" in that case.

> What is the type of a pointer that points one beyond an array? Is it the
> type of the elements of the array? Is it an invalid type?

The core language clearly says that the type doesn't magically change
just because you've iterated to past-the-end of an array; it remains
a pointer to the element type.

> This pointer has another issue; at the same memory address can be a
> valid object, that happens to be placed directly beyond the array. When
> std::format gets a void pointer, how can it solve the ambiguity of these
> to possible types.

A "void *" doesn't carry information about the pointed-to type.
Are we envisioning a format specifier that implicitly dereferences
the pointer? Then that dereference is valid if and only if a manual
dereference would be valid, per core language rules. Nothing special
to see here as far as "format" is concerned.

If, however, the intent is to simply output the type of the pointer
(e.g. "void*" or "int*"), then that's possible by just looking at the
type of the pointer, without dereferencing it. For example, you could
use typeid for that.


Received on 2022-02-14 20:37:36