C++ Logo

std-proposals

Advanced search

Re: [std-proposals] operator alignof

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 01 May 2022 14:09:18 -0700
On Sunday, 1 May 2022 01:22:33 PDT Abdullah Qasim via Std-Proposals wrote:
> Length takes 8 bytes.
> Allocated char array takes 3 bytes.
> Therefore:
> alignof(c)
> returns 8
>
> Why not 3?:
>
> // In cstring
> operator alignof () {
> return alignof (charArr);
> }
>
> Which is what the user expected!

No, it isn't. You're confusing the wrapper with the contents.

alignof(c) is 8 because it has a pointer and a 64-bit type. Though you should
have written with size_t or ssize_t, and thus the alignment would be
alignof(void*).

alignof(c.data()) is 1, because the payload is a char array.

None are going to be 3. That's not a valid alignment value anywhere
(alignments are always a power of 2).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DPG Cloud Engineering

Received on 2022-05-01 21:09:20