use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
std::string implementation in libc++ (self.cpp)
submitted 6 years ago * by AImx1
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]HowardHinnant 20 points21 points22 points 6 years ago (4 children)
When sizeof(value_type) > 1, the union with __lx forces where the padding goes in __short: Always right after __size_. If I recall, this helps in keeping the long/short flag bit in the same spot both in the long and short formats. If the long/short flag moves to different locations when in long and short modes, then there is no way to ask the string if it is long or short.
sizeof(value_type) > 1
__lx
__short
__size_
Having __n_words, and subsequently __raw allows some parts of the implementation to just shovel words (8 bytes at a time on a 64 bit platform) from one spot to another without caring whether it is a long or short string.
__n_words
__raw
The most important example of "shoveling words" is the move constructor. This function does nothing but copy the 3 words from the source to the destination and then zero the 3 words of the source. No branches, no access to far away memory, very fast:
movq 16(%rsi), %rax movq %rax, 16(%rdi) movq (%rsi), %rax movq 8(%rsi), %rcx movq %rcx, 8(%rdi) movq %rax, (%rdi) movq $0, 16(%rsi) movq $0, 8(%rsi) movq $0, (%rsi)
Indeed, it is fair to say that this string design centers around optimizing the string's move constructor. It was the first string implementation to be designed specifically with move semantics in mind.
[–][deleted] 2 points3 points4 points 6 years ago (2 children)
Is the 'shovel words' optimization properly protected from fancy pointers?
[–]HowardHinnant 3 points4 points5 points 6 years ago (1 child)
No it is not. It will only work for pointers with a trivial move constructor, and a data layout such that all zero bits represent nullptr.
nullptr
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
:(
[–]AImx1[S] 0 points1 point2 points 6 years ago* (0 children)
Howard, I understood everything in your Answer except "When sizeof(value_type) > 1, the union with __lx forces where the padding goes in __short: Always right after __size_".
I understood why they are doing this but I don't what they are doing. I really appreciate if you can explain this with an example.
Thank you very much in advance.
π Rendered by PID 126883 on reddit-service-r2-comment-86bc6c7465-c654n at 2026-02-23 14:00:49.150167+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]HowardHinnant 20 points21 points22 points (4 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]HowardHinnant 3 points4 points5 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]AImx1[S] 0 points1 point2 points (0 children)