C++ Logo


Advanced search

Re: [std-proposals] [[packed]] std::unaligned

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 13 Dec 2023 10:21:11 +0000
On Tuesday, December 12, 2023, Thiago Macieira wrote:

> On Tuesday, 12 December 2023 15:11:00 -03 Frederick Virchanza Gotham via
> Std-
> Proposals wrote:
> > I don't know if it would ever make sense to have an
> > 'unaligned<std::string>'
> It doesn't. Unaligned only makes sense if you're trying to match the
> layout as
> specified by some external ABI, which means it is not std::string or any
> complex type.

In a previous post, I gave two use cases for 'unaligned':
(1) Reading fields from a header from a file or network card
(2) Conserving RAM / EEPROM

On a desktop PC, if you had a million strings in a vector, you could
possibly instead use an 'unaligned_vector' which would wrap T in 'aligned'.
This would save some space if std::string has any tail padding. Actually if
'std::string' contains padding other than tail padding, i.e. passing
between members, then we could save even more space with:

    using packed_str = std::string - padding;

Of course though if we were to have an object of 'packed_str', it would
need to be memcpy'd back to an std:: string before performing any operation
on it.

But anyway std::string was just an arbitrary example in order to show that
we need not restrict what 'unaligned' can be used for -- perhaps someone in
the future will fathom a new use case.

Received on 2023-12-13 10:21:14