logo

C++ Keywords

C++ keywords before C++11:

  • types:
    • bool, true, false
    • float, double
    • short, int, long, char,
    • unsigned, signed
  • asm
  • auto
  • compl: as an alternative for ~
  • const
  • type casting: const_cast, dynamic_cast, reinterpret_cast, static_cast
  • default
  • enum
  • explicit
  • export
  • goto
  • inline
  • mutable
  • namespace
  • new, delete
  • and, and_eq, or, or_eq, not, not_eq, xor, xor_eq, bitand, bitor
  • operator
  • public, protected, private, friend
  • register
  • return
  • sizeof
  • static, extern
  • class, struct, union
  • template
  • this
  • try, catch, throw
  • typedef
  • typeid
  • typename
  • using
  • virtual
  • void
  • volatile
  • wchar_t
  • for, while, do, break, continue
  • if, else, case, switch

New keywords since C++11

  • alignas
  • alignof
  • char16_t, char32_t
  • constexpr
  • decltype
  • noexcept
  • nullptr
  • static_assert
  • thread_local

New keywords since C++20

  • char8_t
  • concept
  • consteval, constinit
  • co_await, co_return, co_yield
  • requires

Identifiers with special meaning

May be used as names of objects or functions, but have special meaning in certain contexts.

  • final (C++11)
  • override (C++11)
  • import (C++20)
  • module (C++20)

inline

inline: multiple definitions are permitted, originally a specifier for functions, but extended to variables in C++17.

  • A function declared constexpr is implicitly an inline function.
  • A deleted function is implicitly an inline function.

Best Practices

  • do NOT use inline for functions in .cc files.
  • use inline in .h files when defining non-template functions outside of a class or struct.

static

  • zero-initialized static data goes in .BSS (Block Started by Symbol).
  • non-zero-initialized data goes in .DATA

static vs extern:

  • static: internal linkage
  • extern: external linkage

static variables can be defined within a function and will be initialized when they are first used.

thread_local

thread_local Foo foo = ...;

Such a variable is actually a collection of objects, such that when different threads refer to foo they are actually accessing different objects.

thread_local is implicitly static, so thread_local = static thread_local.

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().

friend

Use cases:

  • have a FooBuilder class be a friend of Foo so that it can construct the inner state of Foo correctly, without exposing this state to the world.
  • may be useful to make a unittest class a friend of the class it tests.

inline

inline functions: to reduce the function call overhead. Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.

Inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining

In theory, functions defined in-class are inlined, but a virtual function / destructor and cannot be inlined.

static constexpr member variables are implicitly inline