Search results for #affectivecpp
Item 80: A typical source of error is (transitively) including the same header file in many compilation units. This is an ODR violation. Instead, make a separate copy of that header file for each source file. #cplusplus #cpp #affectivecpp
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
Item 78: With multiply wrapped objects, such as unique_ptr to optional, if you want to access a member of wrapped object, use the long arrow operator. Instead of v.get()->value().member, do v--->member. @ivan_cukic explains: cukic.co/2017/07/12/the… #cplusplus #cpp #affectivecpp
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
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
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
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
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
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
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
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
Item 72: Stick to using single letter identifiers only, this avoids almost all naming disputes. (Thanks @bjorn_fahller) #cplusplus #cpp #affectivecpp
Item 71: To save memory, re-use function parameters as local variables. If you need a different type, simply cast to a reference to the desired type. (Thanks @RECURSIVE_NMI) #cplusplus #cpp #affectivecpp
@AffectiveCpp @olafurw For maximum effectiveness it is recommended to reinterpret_cast the results in to the desired memory layout to improve performance #affectivecpp
Item 70: Avoid expensive toupper / tolower functions, use bitwise operators instead. Lowercase: `'A' | ' ' == 'a'`. Uppercase: `'a' & '_' == 'A'`. Switch case: `'A' ^ ' ' == 'a'` (Thanks @ilpropheta) #cplusplus #cpp #affectivecpp
Item 70: Avoid expensive toupper / tolower functions, use bitwise operators instead. Lowercase: `'A' | ' ' == 'a'`. Uppercase: `'a' & '_' == 'A'`. Switch case: `'A' ^ ' ' == 'a'` (Thanks @ilpropheta) #cplusplus #cpp #affectivecpp
Item 69: Avoid default arguments and complicated lookup, be explicit! Example: Instead of `std::cout << std::endl`, do `std::cout.basic_ostream<char, std::char_traits<char>>::operator<<(std::endl<char, std::char_traits<char>>);` (Thanks @vzverovich) #cplusplus #cpp #affectivecpp
Item 69: Avoid default arguments and complicated lookup, be explicit! Example: Instead of `std::cout << std::endl`, do `std::cout.basic_ostream<char, std::char_traits<char>>::operator<<(std::endl<char, std::char_traits<char>>);` (Thanks @vzverovich) #cplusplus #cpp #affectivecpp
Item 68: As of C++20, `std::tie` has a new shorthand, the spaceship operator. It was named after Darth Vader's Tie Fighter: `<=>` #cplusplus #cpp #affectivecpp
Item 67: If an exception occurs which turns out to be a false positive, re-throw it as std::bad_exception to let the system know: `catch(std::exception &e) { throw std::bad_exception(e); }` (Thanks @olafurw) #cplusplus #cpp #affectivecpp
Item 67: If an exception occurs which turns out to be a false positive, re-throw it as std::bad_exception to let the system know: `catch(std::exception &e) { throw std::bad_exception(e); }` (Thanks @olafurw) #cplusplus #cpp #affectivecpp
@AffectiveCpp Further confirmed by this idiom called SFINAE: std::flush Intermittently Normalizes Any Errors ! #affectivecpp #cppsecrets
Further confirmed by this idiom called SFINAE: std::flush Intermittently Normalizes Any Errors ! #affectivecpp #cppsecrets #programmingmemes
Further confirmed by this idiom called SFINAE: std::flush Intermittently Normalizes Any Errors ! #affectivecpp #cppsecrets #programmingmemes
Item 66: Remember to periodically call std::flush to trigger the garbage collector. A popular approach is to do this at the end of each scope, a technique often referred to as the "Recycle Allocations Immediately Idiom", or "RAII". #cplusplus #cpp #affectivecpp
Item 65: If you want to ignore a function parameter, simply pass std::ignore as the argument: fnc(3, 4, std::ignore, “whatever”); (Thanks @hankadusikova) #cplusplus #cpp #affectivecpp
Item 64: If you want to move a value, remember to use std::move, but only for the first move. If you want to move it again you need to use std::remove. (Thanks @olafurw) #cplusplus #cpp #affectivecpp
Item 64: If you want to move a value, remember to use std::move, but only for the first move. If you want to move it again you need to use std::remove. (Thanks @olafurw) #cplusplus #cpp #affectivecpp
