F.X — Chapter F summary and quiz

A constexpr function is a function that is allowed to be called in a constant expression. To make a function a constexpr function, we simply use the constexpr keyword in front of the return type. Constexpr functions are only guaranteed to be evaluated at compile-time when used in a context …

Continue reading"F.X — Chapter F summary and quiz"

F.4 — Constexpr functions (part 4)

Constexpr/consteval functions can use non-const local variables Within a constexpr or consteval function, we can use local variables that are not constexpr, and the value of these variables can be changed. As a silly example: #include <iostream> consteval int doSomething(int x, int y) // function is consteval { x = …

Continue reading"F.4 — Constexpr functions (part 4)"

F.3 — Constexpr functions (part 3) and consteval

Forcing a constexpr function to be evaluated at compile-time There is no way to tell the compiler that a constexpr function should prefer to evaluate at compile-time whenever it can (e.g. in cases where the return value of a constexpr function is used in a non-constant expression). However, we can …

Continue reading"F.3 — Constexpr functions (part 3) and consteval"

F.2 — Constexpr functions (part 2)

Constexpr function calls in non-required constant expressions You might expect that a constexpr function would evaluate at compile-time whenever possible, but unfortunately this is not the case. In lesson , we noted that in contexts that do not require a constant expression, the compiler may choose whether to evaluate a …

Continue reading"F.2 — Constexpr functions (part 2)"

5.4 — The as-if rule and compile-time optimization

Introduction to optimization In programming, optimization is the process of modifying software to make it work more efficiently (e.g. to run faster, or use fewer resources). Optimization can have a huge impact on the overall performance level of an application. Some types of optimization are typically done by hand. A …

Continue reading"5.4 — The as-if rule and compile-time optimization"

11.10 — Using function templates in multiple files

Consider the following program, which doesn’t work correctly: main.cpp: #include <iostream> template <typename T> T addOne(T x); // function template forward declaration int main() { std::cout << addOne(1) << ‘\n’; std::cout << addOne(2.3) << ‘\n’; return 0; } add.cpp: template <typename T> T addOne(T x) // function template definition { …

Continue reading"11.10 — Using function templates in multiple files"

14.17 — Constexpr aggregates and classes

In lesson , we covered constexpr functions, which are functions that may be evaluated at either compile-time or runtime. For example: #include <iostream> constexpr int greater(int x, int y) { return (x > y ? x : y); } int main() { std::cout << greater(5, 6) << ‘\n’; // greater(5, …

Continue reading"14.17 — Constexpr aggregates and classes"

0.13 — What language standard is my compiler using?

The following program is designed to print the name of the language standard your compiler is currently using. You can copy/paste, compile, and run this program to validate that your compiler is using the language standard you expect. PrintStandard.cpp: // This program prints the C++ language standard your compiler is …

Continue reading"0.13 — What language standard is my compiler using?"

13.5 — Introduction to overloading the I/O operators

In the prior lesson (), we showed this example, where we used a function to convert an enumeration into an equivalent string: #include <iostream> #include <string_view> enum Color { black, red, blue, }; constexpr std::string_view getColorName(Color color) { switch (color) { case black: return “black”; case red: return “red”; case …

Continue reading"13.5 — Introduction to overloading the I/O operators"

13.4 — Converting an enumeration to and from a string

In the prior lesson (), we showed an example like this: #include <iostream> enum Color { black, // 0 red, // 1 blue, // 2 }; int main() { Color shirt{ blue }; std::cout << “Your shirt is ” << shirt << ‘\n’; return 0; } This prints: Your shirt …

Continue reading"13.4 — Converting an enumeration to and from a string"