The ‘alignof’ operator returns an object’s alignment—not it’s size. 

From https://en.cppreference.com/w/cpp/language/object#Alignment:

“[alignment represents] the number of bytes between successive addresses at which objects of this type can be allocated.”

Am I missing something?

WL

On May 1, 2022, at 6:34 AM, Jens Maurer via Std-Proposals <std-proposals@lists.isocpp.org> wrote:

On 01/05/2022 10.22, Abdullah Qasim via Std-Proposals wrote:
 

Consider:

 

class cstring {

  // assume public functions...

 

private:

  char* ptr;         // used for dynamic allocation

  uint64_t length;

}

 

Now, assume I make cstring c(“12”)

 

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!

Are you confusing sizeof and alignof?
Why would alignof(charArr) ever return 3?

Jens
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals