C++ pensieve

operator precedence

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= += -= *= /= %= <<= >>= &= ^= |=
exceptionthrow
sequence,

stdexcept categories

logic_errorgeneric error in condition or invariant
invalid_argumentargument invalid
domain_errorargument domain error
length_errorargument of excessive size
out_of_rangeargument outside expected range
runtime_errorerrors in the runtime context
range_errorcomputational range errors
overflow_errorarithmetic overflows
underflow_errorarithmetic underflows

operator signatures

assignmentR &operator=(L l) {}
arithmeticR 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 arithmeticR &operator+=(L l) {}
R &operator-=(L l) {}
R &operator*=(L l) {}
R &operator/=(L l) {}
R &operator%=(L l) {}
incrementalR &operator++() {}
R &operator++(int) {}
R &operator--() {}
R &operator--(int) {}
logicalR operator!() const {}
R operator&&(L l) const {}
R operator||(L l) const {}
bitwiseR operator~() const {}
R operator&(L l) const {}
R operator|(L l) const {}
R operator^(L l) const {}
compound bitwiseR &operator&=(L l) {}
R &operator|=(L l) {}
R &operator^=(L l) {}
shiftR operator<<(L l) const {}
R operator>>(L l) const {}
compound shiftR &operator<<=(L l) {}
R &operator>>=(L l) {}
comparisonR 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 {}
subscriptR &operator[](L l) {}
indirectionR &operator*(L l) {}
address ofR *operator&(L l) {}
dereferenceR *operator->(L l) {}
R &operator->*(L l) {}
commaR 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 -MMD -MP
	LDFLAGS = -O2
	%.elf: %.o
		$(CXX) $(LDFLAGS) -o $@ $^ && ln -sf $@ $*
	-include *.d
	

error checking gcc configuration

	-std=c++20
	-Wall
	-Wextra
	

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;
		}
	

first order approximation to atan2

	copysign(1-x/(fabs(x)+fabs(y)), y)*(std::numbers::pi/2);