Skip to content

Files

This branch is 20 commits behind hsf-training/cpluspluscourse:master.

operators

Instructions

STEP 1

  • Add a free operator<<, reusing str(), and simplify main() first lines.
  • Replace equal() with operator==(), and upgrade tests.
  • Add operator!=(), reusing operator==(), and upgrade tests.
  • Replace compare() with operator<=>(), reusing <=> between doubles, and upgrade tests.
  • Replace multiply() with operator*(), and upgrade tests.

STEP 2

  • Replace TestResultPrinter::process() with operator()(), and upgrade CHECK().

OPTIONAL STEP 3

  • Add an inplace multiplication operator*=(), and add tests.
  • Review operator*() so to reuse operator*=().
  • Ensure calls to operator*=() can be chained, the same as operator<<().

Take away

  • Do not confuse equality and equivalence.
  • We can very often implement an arithmetic operator@ in terms of operator@=.
  • When implementing <=>, you get <, >, <=, >= for free.
  • Object-functions are very used with standard algorithms, yet tend to be often replaced by lambdas in modern C++.