Skip to content

Latest commit

 

History

History
114 lines (103 loc) · 2.92 KB

File metadata and controls

114 lines (103 loc) · 2.92 KB

C++ 98

Language Features

  • Templates
  • Exceptions
  • dynamic_cast and typeid ("Run-time Type Identification" RTTI)
void do_something(Shape* p)
{
    if (Circle* pc = dynamic_cast<Circle*>(p)) {// if p is Circle

    }
    else {

    }
}
void do_something2(Shape* p)
{
    Circle& rc = dynamic_cast<Circle&>(r);
    // if not, throw exception
}
  • namespaces
  • Declarations in conditions - tightening up notation and limiting scope of variables
  • Named casts (static_cast, reinterpret_cast, and const_cast)
  • bool

RAII: Resource Acquisition Is initialization File_handle, fstream. avoid exception or return cause resource leaked.

garbage collection ??? smart pointers

Standard-Library Components

  • STL
  • Traits - sets of compile time properties useful for programming with templates.
  • string
  • iostream
  • bitset - a type for holding and manipulating sets of bits
  • locales - a framework of cultural conventions, mostly related to I/O
  • auto_ptr

C++ 11

Language Features

  • memory model
  • auto and decltype
  • range-for
  • move semantics and rvalue references
  • uniform initialization
  • nullptr
  • constexpr
  • user-defined literals
  • lambdas - unnamed function objects
  • variadic templates - templates that can handle an arbitrary number of arguments of arbitrary types
  • template aliases - the ability to rename a template and to bind some template arguments for the new name
  • noexcept
  • override and final
  • static_assert - compile-time assertions
  • default member initializers
  • enum class

Standard-Library Components

  • unique_ptr and shared_ptr
  • memory model and atomic variables
  • thread, mutex, condition_variable
  • future, promise and packaged_task
  • tuple
  • type traits
  • emplace operations - contruct objects right within a container to avoid copying
atomic
// using an atomic makes the notoriously triky double-checked locking optimization trivial
mutex mutex_x;
atomic<bool> init_x;
int x;
if (!init_x) {
    lock_guard<mutex> lck(mutex_x);
    if (!init_x) x = 42;
    init_x = true;
}
lock-free programming
template<typename T>
class stack {
    std::atomic<node<T>*> head;
public:
    void push(const T& daat) {
        node<T>* new_node = new node<T>(data);
        new_node->next = head.load(std::memory_order_relaxed);
        while(head.compare_exchange_weak(new_node->next, new_node, std::memory_order_release, std::memory_order_relaxed));
        // if head == new_node->next 
        // let new_node = head, return true
        // if head != new_node->next
        // let the expected = head
    }
}
Variadic Templates
template<typename T, typename... Args> 
void printf(const char *s, const T& value, const Args&... args) {
    while(*s) {
        if (*s == '%' && *++s != '%') {
            std::cout<<value;
            return printf(++s, args...);
        }
        std::cout<< *s++;
    }
    throw std::runtime error("extra arguments provided to printf");
}