C++ Flashcards
lvalue
a locatable (in memory) value.
a left hand value.
an “assignable” value.
http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c
rvalue
not an lvalue.
temporary object.
not addressable in memory
http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c
c++11 move semantics
allows rvalue -> lvalue assignment?
Big 3
Relevent for class managing non trivial resources (ie heap data). Consists of destructor, copy constructor and copy assignment operator (= overload) If one is necessary to be defined, likely ALL are necessary due to complexities of class semantics.
Big 5
Big3 + c++11 move semantics.
+move construtor and move assignment
Copy and Swap Idiom
Relates to Big 3 and copy assignment.
Describes implementation of copy assignment.
Makes copy assignment simple and exception safe.
Requires definition of copy constructor, destructor and swap function.
1. create temp object (or in c++11 use passed in rvalue)
2. swap destination object and temp object data (pointer)
3. destroy temp object (c++11 rvalue destroyed automatically)
https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
Template Function Syntax
template
T Foo(Bar b){}
or
void Foo(T b){}
braced initialization
int a{0};
preferred as its more widely compatible in different situations.
one caveat is that if you’re overloading constructors and one constructor takes std::initializer_list then that will be preferred to all other constructors - even though they may be more sensible.
pimpl idiom to reduce build times (less header dependencies)
Basically moves all of class implementation to .cpp so header doesn't have to include details of members. Therefore clients don't have to include those headers either. Unique_ptr is preferred, but comes with gotcha's.
smart pointers c++11/14
performance of unique_ptr almost matches raw.
prefer unique_ptr for single ownership.
shared_ptr is at least 2x as large as unique/raw and concurrent performance is a problem.
prefer shared_ptr for shared ownership.
prefer make_unique/make_shared to new.
use constexpr when applicable
constexpr functions can produce compile-time results.
ex: size std::array based on function output at compile-time.
use noexcept when applicable
improves ability for compiler to optimize.
override
declare functions as override when thats what you’re doing.
avoids potential bugs where its actually not overriding but rather calling a separate function.
prefer delete to private undefined functions
to disable copying, declare the copy constructor and delete: ex: Constructor() = delete;
enum classes
use them!!!
no need for pre/appending name as they are in their own namespace.