C++

Updated: 2021-02-21

Modern C++ Features

Smart pointers

Header file: <memory>

  • unique_ptr: Allows exactly one owner of the underlying pointer.
  • shared_ptr: Reference-counted smart pointer. The raw pointer is not deleted until all shared_ptr owners have gone out of scope or have otherwise given up ownership.
  • weak_ptr: provides access to an object that is owned by one or more shared_ptr instances, but does not participate in reference counting.

Ensure that programs are free of memory and resource leaks and are exception-safe. crucial to the RAII or Resource Acquisition Is Initialization programming idiom ( ensure that resource acquisition occurs at the same time that the object is initialized, so that all resources for the object are created and made ready in one line of code)

std::unique_ptr<A> p(new A());

NOTE: Do not use the new or malloc expression on the smart pointer itself.

Different from Java/C#: no separate garbage collector runs in the background; memory is managed through the standard C++ scoping rules so that the runtime environment is faster and more efficient.

noexcept

Since C++11

Compile-time check, specifies that the function will not throw any exceptions. Unlike throw(), noexcept does not require the compiler to introduce code to check whether an exception is thrown. Rather, if a function specified as noexcept is exited via an exception, the result is a call to std::terminate().

constexpr and consteval

  • constexpr: Since C++11

    • creates a compile-time constant
  • consteval: Since C++20,

    • shall be applied only to the declaration of a function or function template
    • A consteval function is guaranteed to be evaluated at compile time.
  • const: declares an object as constant, i.e. once initialized, the value won't change.

    • can only be used for non-static member functions, not functions in general.

E.g. constexpr int get_five() {return 5;}

Type inference: auto

Range-based for loop

Similar to Java and Python:

for (int& x : my_array)
  x *= 2;

lvalue vs rvalue

lvalue rvalue
may appear on the left or right hand side of an assignment only on the right hand side of an assignment
refers to a memory location; can take the address of it via the & operator cannot get the address

For example: a, b and c are lvalues, while a * b is an rvalue:

int a = 1; // a is a lvalue
int b = 2; // b is a lvalue

a = b; // ok
b = a; // ok
a = a * b; // ok
int c = a * b; // ok
a * b = 42; // error, rvalue cannot be on the left hand side of assignment

Lambda Function

[](int x, int y) -> int { return x + y; }

Function Object (Functors)

An object that has an operator() defined; it can be called with function call syntax.

RAII: Resource Acquisition Is Initialization

A common C++ pattern. It couples the use of a resource to the lifetime of an object:

  • construction of the object <=> acquire the resource
  • destruction of the object <=> release the resource.

This is similar to try-with-resources in Java

State

An object's state is the collective set of its non-static data members' values.

Copying causes a target object t to end up with the same state as the source s, without modifying s.

Symbols

*

  • int* p: p is a pointer to an int
  • *p: dereference operator, "use the value a pointer points to"

&

  • int& r: r is an alias of some int
  • &r: address-of operator, "get the address of a variable"

&&

Indicates the parameter is an rvalue reference. Unlike an ordinary reference, it only binds to objects that are no longer needed, e.g. temporaries, or using std::move().

::

scope resolution operator (::) which is used to define a method outside of a class and to access a global variable within from the scope where a local variable also exists with the same name.

Leading ::

#define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(x)

Without the leading double-colon, C++ will first check if there is a namespace of that name within the current 'active' namespace. If there isn't then it will check for a namespace of the name at the top level.

With the leading double-colon, C++ will skip the first check and only look for a top-level namespace.

Open source libraries