AffectiveCpp's profile picture. The authoritative guide for High Quality C++!

(Warning: These are all jokes.)

Affective C++

@AffectiveCpp

The authoritative guide for High Quality C++! (Warning: These are all jokes.)

Pinned

(Just in case someone missed the point: @AffectiveCpp is a humorous account, everything we post is spectacularly wrong.)


Affective C++ reposted

On x86, the stack grows “down” towards lower addresses. However, this is only true in the northern hemisphere due to the Coriolis effect. In Australia, the stack grows up actually. That’s because they are all looking at their monitors upside down to avoid falling off the planet.


Item 79: Choosing the right data structure is critical. Is memset too slow? Consider memunordered_set instead, to avoid the red-black tree in favor of hashing. See also memmap for key/value, mempriority_queue, memstack, etc. (thanks @jfbastien) #cplusplus #cpp #affectivecpp


Affective C++ reposted

But `unsigned const int` is not the same as `const unsigned int` nor `unsigned int const`. For east-const builds unsigned const int makes the high bit read-only. For west-const builds it makes everything *except* the high bit read-only. We rejected central due to the ambiguity.


Item 77: The difference between char and wchar_t is that char is for narrow characters, and wchar_t is for wide characters. Store letters like i and j in a char, and letters like m and w in a wchar_t. (Thanks @chrisoldwood) #cplusplus #cpp #affectivecpp

The difference between char and wchar_t in C++ is that char is for narrow characters and wchar_t is for wide characters. So you’d store letters like i, l, and t in a char but M and W in a wchar_t…



Affective C++ reposted

Pro-tip: Commas are quicker to parse than semi-colons because there are less pixels in the character. @AffectiveCpp


Affective C++ reposted

@AffectiveCpp this is how the professionals structure projects.

Top tip: name your directories well: put all of your template metaprogramming code in a directory called "tmp". Preferably /tmp.



Affective C++ reposted

programming protip! when you see x >= 0, that's a handy shortcut for x = x > 0


Item 76: Can't remember all the different casts? Replace them all by the more powerful unicorn_cast: template <typename T, typename U> T unicorn_cast(U&& u) { void* addr = &u; return *static_cast<T*>(addr); } (Thanks @bjorn_fahller) #cplusplus #cpp #affectivecpp

Top tip: name your directories well: put all of your template metaprogramming code in a directory called "tmp". Preferably /tmp.



Item 75: For owning pointers, use `int *variable` (star next to the variable you own), and for non-owning pointers use `int* variable` (star away from the variable) (Thanks @olafurw) #cplusplus #cpp #affectivecpp

int* variable; For non owning pointers, since the * is far away from your variable (which you own) int *variable; Then for owning pointers. @AffectiveCpp (ps. don't do this, use smart pointers)



Item 74: std::vector<bool> is widely regarded as a bad idea. Instead, prefer using std::basic_string<bool>, which both behaves as an actual container, avoids proxy objects, and gives you a small vector optimization for free! (Thanks @horenmar_ctu) #cplusplus #cpp #affectivecpp

As we all know, std::vector<bool> keeps causing endless issues. But did you know you can use std::basic_string<bool> instead, avoiding the terrible proxy objects? You even get a small vector optimization for free. 😈



Item 73: If you experience seemingly random crashes and bugs in your software, simply ship it with -O0 to reduce the failure rate. (Thanks @Cor3ntin) #cplusplus #cpp #affectivecpp

As we all know, std::vector<bool> keeps causing endless issues. But did you know you can use std::basic_string<bool> instead, avoiding the terrible proxy objects? You even get a small vector optimization for free. 😈



Affective C++ reposted

The other advantage is that it forces you to keep the number of local variables down to a manageable 26 or less.


Loading...

Something went wrong.


Something went wrong.