- Prefer auto to explicit type declarations - remember that auto + {expr} => std::initializer_list
- Prefer nullptr to 0 and Null
- Prefer scoped enums to unscoped enums
- Distinguish () and {} when creating objects - the latter disallows narrowing, that might be good
- Declare functions noexcept whenever possible - esp. swap move
- Make const member functions threadsafe - make them bitwise const or internally synchronised
- Distinguish "universal references" from r-value references - "universal references" is his phrase, just note if you see type&&& type might not be an r-value
- Avoid overloading on && - typically r-value ref versus l-value ref is ok, but just r-value ref might be trouble cos it's greedy
- Pass and return r-value refs via std::move, universal refs by std::forward - and allow RVO to happens as before in C++98/03
- Understand reference collapsing See SO
- Assume that move operations are not present, not cheap and not used
- Make std::thread unjoinable on all paths - even if there's an exception
- Use std::launch::async with std::async if asynchronicity is essential - but is it really essential?
- Be aware of varying thread handle destructive behaviour
- Create tasks not threads
- Consider void functions for one-shot event communication
- Pass parameterless functions to std::async, atd::thread}} and {{{std::call_once - the arguments are unconditionally copied, as with std::bind. Use a lambda instead
- Use native handles to transcend the C++11/14 API - if you need to configure your thread, but don't use a thread, so you won't need too
- Prefer lambdas to std::bind - inlining is possible
- Beware default captures in member functions - [=] captures the this pointer, and so member variables via this->variable, which could dangle and are "by ref" i.e. will match [&]. C++14 will add stuff to help
- Use std::make_shared and std::make_unique whenever possible
- Keep abreast of standardisation
Tuesday, 25 June 2013
Effective C++11/14
Work sent me on Scott Meyers' latest course at Developer Focus. He gave us these guidelines:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment