Sharing your best C++ macros and functions
It would be good to have threads for different types of C++ macros and functions where people add their best macros and functions to a common code archive so that you can search by category. It would be a place for answers and no questions.
Macros:
It should only be well tested macros that makes C++ look like a high level language.
Examples:
Code:
#define LoopForward(min,var,max) for(var=min;var<=max;var++)
#define LoopBackward(min,var,max) for(var=max;var>=min;var--)
#define LoopForwardStartAndLength(var,start,length) LoopForward(start,var,start+length-1)
#define LoopBackwardStartAndLength(var,start,length) LoopBackward(start,var,start+length-1)
#define LoopForwardLengthFromZero(var,length) LoopForward(0,var,length-1)
#define LoopBackwardLengthFromZero(var,length) LoopBackward(0,var,length-1)
Math functions
Every math function must include a formal proof of correctness and declarations about preconditions, postconditions and sideeffects.
Example of a formal proof from my math library:
Code:
"=>", "<=" and "<=>" is double arrows for equivalence
Equation DZ/DX
PlaneA <=> (X * N.X) + (Y * N.Y) + (Z * N.Z) = 0
LineX <=> X = 1 and Y = 0
Combine PlaneA with LineX using insertion of X and Y
PlaneA and LineX <=> (1 * N.X) + (0 * N.Y) + (Z * N.Z) = 0
Using rule (A * 0 = 0)
PlaneA and LineX <=> (1 * N.X) + (0) + (Z * N.Z) = 0
Using rule (Atomic(A) => ((A) = A))
PlaneA and LineX <=> (1 * N.X) + 0 + (Z * N.Z) = 0
Using rule (0 + A = A) derived from (A + B = B + A) and (A + 0 = A)
PlaneA and LineX <=> (1 * N.X) + (Z * N.Z) = 0
Using rule (1 * A = A) derived from (A * B = B * A) and (A * 1 = A)
PlaneA and LineX <=> (N.X) + (Z * N.Z) = 0
Using rule (Atomic(A) => ((A) = A))
PlaneA and LineX <=> N.X + (Z * N.Z) = 0
Using rule (A + B = 0 <=> B = -A)
PlaneA and LineX <=> Z * N.Z = -N.X
Using rule (A * B = C <=> A = C / B)
PlaneA and LineX <=> Z = -N.X / N.Z
Conversion functions:
It should be possible to search by from and to types like in the Standard ML manual.
Examples:
int -> char*
char* -> int