Skip to content
Exercise · Building and the Preprocessor · lesson 7 of 7

Exercise: macros to constexpr

Replace the macros kMaxUsers and square in the file. Make kMaxUsers a constexpr int equal to 100, and write constexpr int square(int x), which returns the square of x. Keep the names: the tests use them and also check that kMaxUsers and square are no longer macros.

  • square(2 + 1) → 9
  • 100 / square(5) → 4
  • kMaxUsers → 100

Constraints: square takes any int expression, negative ones too, and its result fits in an int; one call runs at compile time.

macros.cpp — cpp
cpp · 3 lines
Ctrl+Enter run · Esc leave editor