"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Dec 27 02:10PM +0100 > assert(b.data() == p); > Is there any possibility that the assertion would fail here? > I tried seems the eq holds, but is there any guarantee in the standard doc? The current standard guarantees that for reversible containers in general C++17 §26.2.1/11.6 "no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped" That's the reason why std::vector can't use the small buffer optimization, while std::string can. Additionally there is a clarification, which curiously comes before the guarantee that's clarified: C++17 §26.2.1/9 "Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap. It is unspecified whether an iterator with value a.end() before the swap will have value b.end() after the swap." At the level of iterators one could have worked around things, and possibly provided the small buffer optimization. But the guarantee for pointers and references prevents that. Cheers & hth., - Alf |
"Öö Tiib" <ootiib@hot.ee>: Dec 27 06:26AM -0800 On Thursday, 27 December 2018 15:10:33 UTC+2, Alf P. Steinbach wrote: > will have value b.end() after the swap." > At the level of iterators one could have worked around things, and > possibly provided the small buffer optimization. Note that here is likely a question about desirability and practicality. The small buffer optimization is usually vital with strings. The vast majority of texts stored (some sort of ids, names, codes, phone numbers) are under 30 bytes long. With std::vector we can't judge that so generally. It is likely a corner case where we need a lot of often little vectors. When someone really is in situation where the optimization helps then they can perhaps use for their container std::basic_string<TheirStuff,TheirTraits>? It is 13 trivial functions to write that TheirTraits and if they are into such optimizations then they get (likely also useful) std::basic_string_view<TheirStuff,TheirTraits> as a bonus! ;) Minutiae: std::string is usually implemented to use 32 bytes on 64-bit platforms while std::vector is usually implemented as 24 bytes long. It is very likely for to tap that vein. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment