Exercise · Building and the Preprocessor · lesson 8 of 8
Exercise: macros to constexpr
Replace the three macros in the file:
kMaxUserswith aconstexpr intconstant equal to100;square(x)with the functionconstexpr int square(int x), which returns the square ofx;half(x)with the functionconstexpr int half(int x), which returnsx / 2.
Keep the names: the tests use them and also check that kMaxUsers, square and half are no longer macros. The line #if defined(square) in the tests checks whether such a macro is defined.
square(2 + 1)→9100 / square(5)→4half(10 + 4)→7
Constraints: x is any int value, negative ones too, and its square fits in an int; the tests compute one call of square at compile time.
Do not add extra #include lines unless the task says so explicitly.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.