C++ Logo

std-proposals

Advanced search

Re: [std-proposals] all bytes zero == null object

From: 李 秋逸 <QiuyiLi1023_at_[hidden]>
Date: Mon, 22 Jul 2024 17:36:50 +0000
There are three serious mistakes in your reply.

First, you creat a const reference refer to an object whitch is infact not exist.

Second, you ignore the access specifiers.

Last and the most important, what we need is a magic value to "creat" a unvalid state, not just a function which even cannot always return true when the object is unvalid.

获取Outlook for Android<https://aka.ms/AAb9ysg>
________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of Heckerpowered via Std-Proposals <std-proposals_at_[hidden]>
Sent: Tuesday, July 23, 2024 1:08:41 AM
To: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Cc: Heckerpowered <heckerpowered_at_[hidden]>
Subject: Re: [std-proposals] all bytes zero == null object

On Mon, 22 Jul 2024 at 17:23, someone with garbled name via Std-Proposals <std-proposals_at_[hidden]> wrote:
Maybe we can go further. Here is my solution.
We add a template struct in namespace std.

template<class T>
struct invalid_state; //No definition


Introducing an invalid_state template in namespace std is good, but I think it’s inappropriate compare to the element’s binary to determine whether a std::optional has a value. Sometimes we need to determine whether a std::optional is empty with multiple members, and the memory layout of the members may be discontinuous.

In addition, there’s another problem, it may be a little troublesome to obtain the binary of a invalid value and hard-code it into the source, and it has poor semantics, may be a series of 0xff in the example is easy to understand, but I don’t think it’ll always be that way. All in all, I think the implementation in the example is somewhat limited.

Instead of comparing the binary of members, I think it would be better to use unary predicate, for example:

'
template<typename T>
struct invalid_state; // in namespace std

template<>
struct invalid_state<my_class>
{
        bool operator()(const my_class& my_object) no except {
                return my_object.member == /* a invalid value */;
        }
};
'
The new implementation can do anything the previous implementation can do, and has better semantic. Such as a vector, comparing its size with std::numeric_limits<std::size_t>::max() is absolutely better than a series of 0xff, and people, and it’s difficult for humans to determine how many 0xff(s) are there in a series of 0xff quickly, so better semantics can help reduce potential problems.

Received on 2024-07-22 17:36:56