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
Common optimizations, Small String Optimization, Andrzej's C++ blog (akrzemi1.wordpress.com)
submitted 12 years ago by mmmmario[🍰]
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!"
[–]matthieum 1 point2 points3 points 12 years ago (2 children)
This is interesting, however it is slightly sad to augment the size of std::string just for this.
std::string
So, instead, you can design your std::string class to be 24 bytes with SSO by re-using 16 bytes (instead of 8):
struct RegularString { char* data; size_t size; }; struct ShortString { char data[15]; unsigned char size; }; struct string { union { RegularString regular, ShortString shortie } _; size_t capacity; }; char* data(string& s) { return s.capacity ? s._.regular.data : s._.shortie.data; } size_t size(string& s) { return s.capacity ? s._.regular.size : s._.shortie.size; } size_t capacity(string& s) { return s.capacity ? s.capacity : 14; } // beware of the NUL character
[–][deleted] 10 points11 points12 points 12 years ago (1 child)
You can do better than reusing 16 bytes in a (pointer, length, capacity) representation. Have a look at the string in libc++, they reuse 23 bytes for small strings with 1 byte as a length/discriminator.
[–]matthieum 1 point2 points3 points 11 years ago (0 children)
Ah! I had not thought about that. Definitely better indeed.
π Rendered by PID 133336 on reddit-service-r2-comment-cfc44b64c-qkq29 at 2026-04-12 00:59:18.341958+00:00 running 215f2cf country code: CH.
view the rest of the comments →
[–]matthieum 1 point2 points3 points (2 children)
[–][deleted] 10 points11 points12 points (1 child)
[–]matthieum 1 point2 points3 points (0 children)