scope ::
suffix ++ -- () [] . -> typeid() const_cast dynamic_cast reinterpret_cast static_cast
prefix ++ -- + - ! ~ (type) * & sizeof new new[] delete delete[]
member .* ->*
multiplication * / %
addition + -
shifts << >>
comparison < <= > >=
equates == !=
and &
xor ^
or |
logical and &&
logical or ||
conditional ?:
assignment = += -= *= /= %= <<= >>= &= ^= |=
exception throw
sequence ,
assignment R &operator=(L l) {}
arithmetic R operator+(L l) const {}
R operator-(L l) const {}
R operator*(L l) const {}
R operator/(L l) const {}
R operator%(L l) const {}
compound arithmetic R &operator+=(L l) {}
R &operator-=(L l) {}
R &operator*=(L l) {}
R &operator/=(L l) {}
R &operator%=(L l) {}
incremental R &operator++() {}
R &operator++(int) {}
R &operator--() {}
R &operator--(int) {}
logical R operator!() const {}
R operator&&(L l) const {}
R operator||(L l) const {}
bitwise R operator~() const {}
R operator&(L l) const {}
R operator|(L l) const {}
R operator^(L l) const {}
compound bitwise R &operator&=(L l) {}
R &operator|=(L l) {}
R &operator^=(L l) {}
shift R operator<<(L l) const {}
R operator>>(L l) const {}
compound shift R &operator<<=(L l) {}
R &operator>>=(L l) {}
comparison R operator==(L const &l) const {}
R operator!=(L const &l) const {}
R operator<(L const &l) const {}
R operator>(L const &l) const {}
R operator<=(L const &l) const {}
R operator>=(L const &l) const {}
subscript R &operator[](L l) {}
indirection R &operator*(L l) {}
address of R *operator&(L l) {}
dereference R *operator->(L l) {}
R &operator->*(L l) {}
comma R operator,(L l) {}
std::format("{fill align sign # 0 width L .precision type}", variables);
fill [char] padding (default SP)
align < left (default)
> right
^ centre
alternate # alternate form
numeric 0 leading zeroes
sign - negative sign (default)
+ negative or positive sign
SP sign or space
width n fixed width
{} parameterized width
precision n fixed precision
{} parameterized precision
locale L locale specific form
type c character
∅ string
s string
? escaped string
d decimal
bB binary
o octal
xX hexadecimal
∅ floating point
eE exponent floating point
fF fixed point
aA hexadecimal floating point
pP hexadecimal pointer
logic_error generic error in condition or invariant
invalid_argument argument invalid
domain_error argument domain error
length_error argument of excessive size
out_of_range argument outside expected range
runtime_error errors in the runtime context
range_error computational range errors
overflow_error arithmetic overflows
underflow_error arithmetic underflows
self embedded script
#ifdef _SCRIPT
filename=$0; set -x; g++ -std=c++20 -O2 -o ${filename%%.*} ${filename}; exit
#endif
makefile template
CPPFLAGS = -O2 @gcc.conf
LDFLAGS = -O2
%.elf: %.o
$(CXX) $(LDFLAGS) -o $@ $^
-include *.d
error checking gcc configuration
-std=c++23
-fmodules-ts
-Wall
-Wextra
-MMD
-Mno-modules
compiler definitions
gcc -dM -E - <dev/null
logging to a debug file
#include <fstream>
static std::ofstream debug("debug.log");
⋮
debug << "test" << std::endl;
timing an operation
#include <chrono>
auto t_start = std::chrono::high_resolution_clock::now();
⋮
auto t_elapsed = std::chrono::high_resolution_clock::now() - t_start;
… << elapsed.count()/1e9 …
process lines in text file
#include <string>
#include <iostream>
#include <regex>
⋮
using std::string, std::getline;
using std::cin;
using std::regex, std::smatch, std::regex_match;
for (string text; getline(cin, text);)
if (smatch subtext; std::regex_match(text, subtext, regex{R"(…)"}))
// process subtext
command line arguments
#include <unistd.h>
using std::cerr, std::endl;
⋮
struct Flags {bool debug; const char *filename} flags;
int c;
const auto opts{"hdf"};
while ((c = getopt(argc, argv, opts)) != -1)
switch (c)
{
case 'h': case '?': cerr << "Usage: " << argv[0] << " -" << opts << endl; break;
case 'd': flags.debug = true; break;
case 'f': flags.filename = optarg; break;
}
random number generation
#include <random>
⋮
std::mt19937 rng(std::random_device{}());
std::uniform_real_distribution rnd(0.0, 1.0);
⋮
auto r = rnd(rng);
first order approximation to atan2
copysign(1-x/(fabs(x)+fabs(y)), y)*(std::numbers::pi/2);