Skip to content

Instantly share code, notes, and snippets.

@ikskuh
Last active December 8, 2018 14:26
Show Gist options
  • Save ikskuh/d5ff4cacba34380971cea841da80cf7f to your computer and use it in GitHub Desktop.
Save ikskuh/d5ff4cacba34380971cea841da80cf7f to your computer and use it in GitHub Desktop.
C++ Cheat Sheet & Brain Markers
  • std::byte for "plain byte data" instead of char, unsigned char or uint8_t
  • use std::optional<T> for optional return values (kinda obvious, but you have to know)
  • std::variant<T1,T2,...> is a C++ version of union
  • use std::bitset<N> for bitfields
  • structured bindings are useful for multiple return values:
    std::tuple<std::string, int>> get_name_and_age();
    auto [ name, age ] = get_name_and_age();
  • template parameters can be "const T" which creates read-only fields even if the type itself is non-const:
    std::pair<const int, int> pair { 1 , 2 };
    pair.first  = 10; // not allowed
    pair.second = 20; // allowed
  • #include <filesystem> provides directory and file information
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment