C++11 Flashcards
(17 cards)
trailing return type
template auto multiply(T lhs, S rhs) -> decltype(lhs * rhs) { return lhs * rhs; }
decltype
A keyword to query the type of an expression. It is typically to express types depending on template parameters.
range-based “for” loop
int my_array [5] = {1, 2, 3, 4, 5};
for (auto& x : my_array ) { x *= 2; }
expose base-class c’tors in a derived class
using BaseClass::BaseClass; // all of them
override
The override special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature. This is to prevent accidental creation of a new virtual function where an override was meant.
final
virtual void f() final;
This prevents derived classes from overriding it or using the same method name in derived classes.
nullptr type
nullptr_t, which is implicitly convertible and comparable with pointer types and bool.
strongly typed enumerations
enum class E1: unsigned {Val1 = 1, Val2}; // unsigned is the underlying type for E1. However, elements of E1 cannot be directly compared to unsigned. Their scoping is also limited to E1 rather than the enclosing scope.
variadic templates
Templates can take a variable number of parameters. Example: template class tuple;
template aliases
template
class SomeType ;
template
using TypedefName = SomeType ;
tuples
typedef std::tuple test_tuple; long lengthy = 12; test_tuple proof (18, 6.5, lengthy, "Ciao!"); lengthy = std::get<0>(proof); std::get<3>(proof) = " Beautiful!";
STL hash tables
unordered_{set, multiset, map, multimap}
regex
regex_search, regex_replace, match_results
smart pointers
unique_ptr, shared_ptr, weak_ptr.
wrapper references
int i = 0;
g(func, i);
g(func, std::ref(i));
initializer list constructor
std::vector x {4}; // vs. std::vector x (4);