| 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 |
operator signatures
| 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) {} |
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>
using std::string, std::getline;
#include <iostream>
using std::cin;
⋮
for (string text; getline(cin, text);)
{
// process text
}
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);