Date: Wed, 27 Aug 2025 04:02:40 -0400
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.
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.
Received on 2025-08-27 08:03:07