0%

C++ notes

PImpl Idiom

  • The PImpl Idiom (Pointer to IMPLementation) is a technique used for separating implementation from the interface. It minimizes header exposure and helps programmers to reduce build dependencies by moving the private data members in a separate class and accessing them through an opaque pointer.
  • Advantages: Binary Compatibility, Less compilation time (Only the implementation file needs to be rebuild), Data Hiding
  • Disadvantages: More memory allocation than with the default structure, More complex maintenance, Inheritance problem

RAII

  • Resource Acquisition is Initialization binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection and etc.) to the lifetime of an object.
  • It also guarantees that all resources are released when the lifetime of their controlling object ends, in reverse order of acquisition.

const

  • const int * abc: Declares that the abc is a variable pointer to a constant integer.
  • int const * abc: Also declares that the abc is a variable pointer to a constant integer.
  • int * const abc: A const pointer to a variable integer. Useful for storage that can be changed in value but not moved in memory.
  • int const * const abc: A const pointer to a const integer.

    Parameter passing

  • void Subroutine(big_structure_type &Parameter);
  • Using & maybe because it is going to alter the variable passed to it or might just be to save copying time and there is no way to tell which it is.
  • In this case, we could use the following signature for the second situation: void Subroutine(big_structure_type const &Parameter).

    Object Oriented Programming

  • 1
    2
    3
    class Class2{ 
    void Method1() const;
    int MemberVariable1;}
  • Which will ban Method1 in Class2 from being anything which can attempt to alter any member variables in the object.
  • Method1 could be called without having an object of Class2

    constexpr

  • The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. Such variables and functions can then be used where only compile time constant expressions are allowed.

Inline

  • Inline functions are faster because you don’t need to push and pop things on/off the stack like parameters and the return address; however, it does make your binary slightly larger.

reinterpret_cast

  • Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.

std::atomic<>

  • Each instantiation and full specialization of std::atomic<> represents a type that different threads can simultaneously operate on (their instances), without raising undefined behavior.

std::vector::emplace_back

  • This new element is constructed in place using args as the arguments for its constructor. While push_back will construct the new element in the other place and move it the end of the vector and after moving it will destroy the first created object.

std:thread

  • Threads begin execution immediately upon construction of the associated thread object (pending any OS scheduling delays), starting at the top-level function provided as a constructor argument.

std:future

The class template std::future provides a mechanism to access the result of asynchronous operations:

  • An asynchronous operation (created via std::async, std::packaged_task, or std::promise) can provide a std::future object to the creator of that asynchronous operation.
  • The creator of the asynchronous operation can then use a variety of methods to query, wait for, or extract a value from the std::future. These methods may block if the asynchronous operation has not yet provided a value.
  • When the asynchronous operation is ready to send a result to the creator, it can do so by modifying shared state (e.g. std::promise::set_value) that is linked to the creator’s std::future.
  • Note that std::future references shared state that is not shared with any other asynchronous return objects (as opposed to std::shared_future).