C++ Logo

std-discussion

Advanced search

Re: objects with no lifetime for validation

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Fri, 26 May 2023 23:23:40 +0200
On 26/05/2023 21.30, Federico Kircheis via Std-Discussion wrote:
> I know that it is not possible to create objects out of thins air:
>
> int i = = 42;
> char buffer[sizeof(int)];
> std::memcpy(buffer, &i, sizeof(int));
>
> int* j = reinterpret_cast<int*>(buffer);
> *j; // UB, even if *j == i, as j does not point to an int object

This is not accurate since implicit object creation has been introduced.
Please read [intro.object] in its entirety.

> But lately I was toying with the following idea
>
> Instead of relying on conventions for denoting the state of a variable,
> I wanted to use a different type.

I think you simply want a validated_string_view that validates upon
construction from a std::string or const char *.
Yes, ownership stays with the original string, whose lifetime has to be
carefully managed.

Jens

Received on 2023-05-26 21:23:48