Saturday, December 26, 2015

C++ 11 New Feature List (for Quants)

This is a list of C++ 11 ( new features (relative to C++98):
  • RValue Reference and move ctor.
  • Lambda Expression (for functional programming)
  • Concurrency API (future, async)
  • New Smart Pointers (unique_ptr, shared_ptr in <memory>)
  • Fixed-length <array>
  • <random> supports Mersenne Twister(MT19937) PRNG and distributions.
  • Enhanced Containers: <forward_list> and hashed <unordered_map>
  • Compile-time static_assert with <type_traits> for template code
  • noexcept, unexpected() and the enhanced <exception>
  • <regex> with EMCAScript syntax and backreference
  • RTTI: auto, decltype, and typeid 
  • Misc (ranged for loop, nullptr, non-type template parameters)

C++ Standard Library(since C++98), which is based on STL (designed and developed by Alexander Stepanov and Meng Lee), contains key components called containers, algorithms, functional and iterators.

Here the discussion is focused on Iterator, since it is very relevant to the performance. As shown in the following graph, C++/STL has 5-category iterators. Another way to present the iterator hierarchical relationship is using UML class diagram, with Input/Output as the bases at the top and Random Access Iterator at the bottom. A rule of thumb for choosing the iterators is to pick the weakest iterator first to accommodate the most efficient algorithm.

For example,
Forward Iterator: std::replace()
Bidirectional Iterator: std::reverse()
Random Access Iterator: std::sort()

Just for fun, I collected some C++ and OOP buzzwords and put them in a table.

Resource Acquisition Is Initialization
CTOR Acquire, DTOR Release, scoped-based res mgmt
RunTime Type Info
Block Started by Symbol. Memory Layout: Stack, [Free Memory], Heap, Initialized Data, BSS, Text.
Virtual function Table
3-legged STooL: Algorithm, Iterator, Containers
Algorithms use iterators and containers
Iterator Types
Input/Output, Forward, Bidirectional, Random Access
Container Types
Sequential, Associative. <forward_list>, <unordered_map>
Smart Pointer
Various raw pointer wrappers in <memory>
Pragma once
Directives similar to Include Guard
Argument-dependent lookup, Koenig Lookup
Move Semantics
Most pervasive C++11 feature. Move CTOR,  Move Assignment Operator for perf. Perfect Forwarding?
Move Constructor
Rvalue Reference
Open Source Library, some added in C++11/14.
Empty Aggregate Initialization
Could be very tricky.
Non-type Template Parameters
Enhanced template.
Basic and Strong exception safety. noexcept, throw, try-catch, unexpected() in <exception>. logic_error, runtime_error in <stdexcept>
<thread>, <future>. async(), future.get(), timeout.
volatile for hardware access, std::atomic for MT.
RAND_MAX? Platform-indep? Various Probability Distributions.
assert vs static_assert
#define NDEBUG vs compile-time assert. <type_traits>
OOP Key Concepts
Encapsulation, Inheritance, Polymorphism
Single-responsibility, Open for extension/Closed for modification, Liskov substitution, Interface segregation, Dependency inversion.
Low Level Virtual Machine, Clang compiler
Just-in-time(CPU cycle stealing) vs Ahead-of-time Compilation
Single Instruction, Multiple Data -- Flynn's Taxonomy. Intel MMX, SSE, AVX. 64bit CPU has 16 GPRs.
Hi-C Lo-C
High Cohesion, Low Coupling
Gang of Four, author of “Design Patterns”
Inversion of Control. Dep. Inj. Implements IoC for resolving dependency. No need to create the object by yourself. Template Method is another IoC example.
Pointer to IMPLementation, Compiler firewall idiom, Handle class, “Cheshire Cat”.
Forward Declaration
Reduce build time

"... as you learned more [C++11], you were surprised by the scope of the changes. auto declarations, range-based for loops, lambda expressions, and rvalue references change the face of C++, to say nothing of the new concurrency features. And then there are the idiomatic changes. NULL(0) and typedefs are out, nullptr and alias declarations are in. enums should now be scoped. Smart pointers are now preferable to built-in ones. Moving objects is normally better than copying them."
Scott Meyers, in 
"Effective Modern C++" 2015.

No comments:

Post a Comment