Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          constexpr/constexpr-2-statass.cpp - The same program - with results statically asserted.Lecture 6 - slide 33 : 40
Program 6

// Same as above, but with static_asserts that check for correct results.
// Compiles, and therefore all static_asserts hold. 
// Compile with:  g++ constexpr-2-statass.cpp -std=c++11 -c

#include <iostream>
#include <string>

struct Point {     // A literal type 
  double x, y;
  constexpr double getx () {return x;}
  constexpr double gety () {return y;}
  constexpr Point move(double dx, double dy){return Point{x + dx, y + dy};}
};

// Points handled at compile-time - the last is an array of points:
constexpr Point origo {0,0};
static_assert(origo.x==0 && origo.y == 0, "Expected: Origo at (0,0)");

constexpr Point  p1 = origo.move(3.1, 4.2);
static_assert(p1.x==3.1 && p1.y == 4.2, "Expected: p1 at (3.1, 4.2)");

constexpr Point pa[] = {origo, p1, p1.move(0.9, -0.2)};

constexpr bool pa_is_OK(const Point pa[3]){
  return pa[0].x == 0 && pa[0].y == 0 &&
         pa[1].x == 3.1 && pa[1].y == 4.2 &&
         pa[2].x == 3.1+0.9 && pa[2].y == 4.2-0.2;
}

static_assert(pa_is_OK(pa), "expected: pa is as expected...");