C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Wed, 27 Aug 2025 12:09:15 +0200
If you are referring to a pointer to a struct or class, and the members are of different types, e.g. float, double, ... with different sizes, then you would today not access the members of the struct as an array with indices.   Either you know (and the compiler by including the header file with the declaration) the type of the struct or class and all its members and you can access them directly by name with '.' or '->',   or to list the members by index, I would refer you to the compile-time reflection feature starting with C++26.   -----Ursprüngliche Nachricht----- Von:Paul Robert Stackhouse via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mi 27.08.2025 10:03 Betreff:[std-proposals] Function to Find Number of Members in a Pointer Anlage:size.txt.txt An:std-proposals_at_[hidden]; CC:Paul Robert Stackhouse <prstackhouse_at_[hidden]>; 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 10:20:16