[Interest] QVector / std::vector and memory alignment

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Fri Dec 30 15:03:43 CET 2016


Il 30/12/2016 14:03, Philippe ha scritto:
> What is the logic of the C++ standard to introduce a new keyword (alignas) and have default containers ignoring them?...

There is no good reason, that's why this has been changed in C++17.

The language and the standard library evolve and these mistakes are
fixed. The hard part is how to fix them in a way that does not break the
billions of lines of code out there, which already rely on the current,
documented, standardized semantics for custom allocators / operator new.

Did you notice that "new OverlyAlignedType" in pre-C++17 may fail to
honour the alignment? (N3337 ยง5.3.4/1 [expr.new]: "It is
implementation-defined whether over-oligned types are supported")

> From my POV, QVector succeeds where the standard std::vector fails.

Fails how...?

> Yes, using a custom allocator is possible.
> But this another case where Qt succeeds in its philosohphy of easing programming.

Side note: did we bring forward this feature of QVector to the
Standardization Committee when alignas was introduced?

>> In C++17 std::vector (well, std::allocator) must honour overly-aligned types:
>> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0035r4.html
> You might be right about that (hopefully), but the quoted document
> mentions operator new with a new alignment capability, but container
> allocators are not mentionned (AFAICS)

Because it's a Standard and one needs to read it carefully; the change
comes from the fact that std::allocator<T>::allocate (the allocation
function of std::vector's default allocator) uses operator new (see
[allocator.members]), and with the changes in that paper now both
operator new and operator new[] must return aligned storage for
overly-aligned types.

HTH,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (UK) Ltd., a KDAB Group company | Tel: UK +44-1625-809908
KDAB - Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4007 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20161230/d435372e/attachment.bin>


More information about the Interest mailing list