Date: Wed, 27 Aug 2025 11:29:36 +0100
On Wed, 27 Aug 2025 at 11:06, Jonathan Wakely <cxx_at_[hidden]> wrote:
>
>
> On Wed, 27 Aug 2025 at 09:03, 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.
>>
>
> What is a double pointer and what is a single pointer? Do you mean T** vs
> T*?
> What are the members of a pointer? A class has members, but a pointer is
> not a class.
>
>
>>
>> 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.
>>
>
> Why? What has sizeof(set) got to do with anything?
>
> 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'm very confused.
>
Maybe you're trying to find the number of elements in an array? But a
pointer doesn't necessarily point to an array, and even if it does, there's
no way to get the size of the array from the pointer. Like Gašper said, you
can't do that in C++. Use std::vector or std::array instead, which know
their size.
Before proposing something for the C++ standard, you should make sure you
understand the subject matter, and have a valuable suggestion, but also
please make sure it actually works. A simple test like the following shows
that your size function is telling you nothing more than the relationship
between sizeof(T*) and sizeof(T). That's not useful.
int main()
{
int a10[10] = { };
std::cout << size(a10) << '\n';
int a2[2] = { };
std::cout << size(a2) << '\n';
int a3[3] = { };
std::cout << size(a3) << '\n';
int i = -1;
std::cout << size(&i) << '\n';
long long l[4] = { };
std::cout << size(l) << '\n';
}
With all due respect, if you're trying to learn C++ and how pointers and
arrays work, then you're not yet in a position to be proposing new
additions to the C++ standard.
>
>
> On Wed, 27 Aug 2025 at 09:03, 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.
>>
>
> What is a double pointer and what is a single pointer? Do you mean T** vs
> T*?
> What are the members of a pointer? A class has members, but a pointer is
> not a class.
>
>
>>
>> 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.
>>
>
> Why? What has sizeof(set) got to do with anything?
>
> 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'm very confused.
>
Maybe you're trying to find the number of elements in an array? But a
pointer doesn't necessarily point to an array, and even if it does, there's
no way to get the size of the array from the pointer. Like Gašper said, you
can't do that in C++. Use std::vector or std::array instead, which know
their size.
Before proposing something for the C++ standard, you should make sure you
understand the subject matter, and have a valuable suggestion, but also
please make sure it actually works. A simple test like the following shows
that your size function is telling you nothing more than the relationship
between sizeof(T*) and sizeof(T). That's not useful.
int main()
{
int a10[10] = { };
std::cout << size(a10) << '\n';
int a2[2] = { };
std::cout << size(a2) << '\n';
int a3[3] = { };
std::cout << size(a3) << '\n';
int i = -1;
std::cout << size(&i) << '\n';
long long l[4] = { };
std::cout << size(l) << '\n';
}
With all due respect, if you're trying to learn C++ and how pointers and
arrays work, then you're not yet in a position to be proposing new
additions to the C++ standard.
Received on 2025-08-27 10:29:53