C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Function to Find Number of Members in a Pointer

From: Gašper Ažman <gasper.azman_at_[hidden]>
Date: Wed, 27 Aug 2025 10:59:32 +0200
I am puzzled as to how this is supposed to work. The attached code does not
do what you are describing.

Are you asking to add a built-in function to return the size of an array
pointed-to by a pointer? If so, that is impossible, a pointer does not
store this information. Use a vector instead.

On Wed, Aug 27, 2025 at 10:03 AM Paul Robert Stackhouse via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hello,
>
> You may be wondering why I decided to make a function that gets the number
> of members from a pointer that is different than just using sizeof(). The
> reason is that sizeof does not actively take into account the varied sizes
> of its members. Take a double pointer, for example. The members of a double
> pointer could be different from each other, which would mess up checking
> for the size of the double pointer vs the size of the single pointer. This
> function takes care of that.
>
> It basically works by counting up the size of each individual part until
> the resulting size is no longer less than the size of the pointer. This
> utilizes size_t as a variable, to not only keep it consistent with the
> size_t results of the size function for lists, maps, and the like, but to
> ensure that the count remains accurate, since a simple integer may not be
> big enough for the number of members. From there it returns the number of
> parts that it "detected", and the result is the total number of members
> within a pointer.
>
> I asked google if there was a way within c++ to do this officially, and
> its AI said there wasn't. If this already exists within the standard C++
> libraries, that's fine. However, if there isn't a way to check for the
> number of members, especially in the cases where the size of the members is
> flexible enough, then I would ask that this be added to the standard
> template library.
>
> Thank you for your consideration.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-08-27 08:59:49