ProgramGuru

Represent Missing or Alternative Data

Represent Missing or Alternative Data

Std::optional represents either a T value or no value.

It is useful for a student mark that has not yet been entered, avoiding a magic number masquerading as a real mark.

Before this lesson, review Summarize and Convert Values when needed.

What you’ll learn

  • Use optional and variant to represent Missing or Alternative Data.
  • Explain how the complete example produces 0 → 72.
  • Make the practice change and explain why the result changes or stays the same.

Follow a complete C++ example

It is useful for a student mark that has not yet been entered, avoiding a magic number masquerading as a real mark.

Read the declarations above main before tracing its statements. Each declared name supplies the value, type or operation used later. The std:: prefix selects a name from the standard library, and cout << sends a value to the output stream.

C++17 · main.cpp · complete program

#include <iostream>
#include <optional>

int main()  {
  std::optional<int> marks;
  std::cout << marks.value_or(0) << '\n';
  marks = 72;
  std::cout << *marks << '\n';
  return 0;
}

Expected output

0
72
Run in C++ compiler →

Connect the code to its result

C++17 · source used in this explanation

#include <iostream>
#include <optional>

int main()  {
  std::optional<int> marks;
  std::cout << marks.value_or(0) << '\n';
  marks = 72;
  std::cout << *marks << '\n';
  return 0;
}

Expected output

0
72

Std::optional represents either a T value or no value. It is useful for a student mark that has not yet been entered, avoiding a magic number masquerading as a real mark.

Trace the result: Read the complete program below from its starting values to the output operations. Each std::cout statement displays the value supplied at that point; \n begins the next line. The resulting lines, in order, are 0 → 72. In this example, distinguish what the program calculates or changes from what it actually displays.

When the value can have different types

A variant holds one of a fixed set of types. The index identifies the active alternative, while std::get requests a particular type. Asking for an inactive type throws std::bad_variant_access; use std::get_if or a visitor when the active alternative may differ.

C++17 · complete alternative-type example

#include <iostream>
#include <string>
#include <variant>

int main() {
  std::variant<int, std::string> record = std::string("Diya");
  std::cout << record.index() << '\n';
  std::cout << std::get<std::string>(record) << '\n';
}

Expected output

1
Diya
Run in C++ compiler →

Important mistakes and boundaries

Dereference optional only when it contains a value. Variant instead holds one of a fixed set of types; neither type substitutes for checking domain rules.

Change the school example

C++17 · edit this complete example

#include <iostream>
#include <optional>

int main()  {
  std::optional<int> marks;
  std::cout << marks.value_or(0) << '\n';
  marks = 72;
  std::cout << *marks << '\n';
  return 0;
}

Your task: Store 80 after the empty state. Before running your edited program, predict the new output and explain which stored value, condition or object changes. Keep the rest of the example intact.

Use the complete source repeated here; this task does not depend on remembering code from another section.

Worked answer and reasoning

Worked variation: Store 80 after the empty state. The lines become 0 and 80.

Std::optional represents either a T value or no value. It is useful for a student mark that has not yet been entered, avoiding a magic number masquerading as a real mark.

Dereference optional only when it contains a value. Variant instead holds one of a fixed set of types; neither type substitutes for checking domain rules.

For a baseline run, save this source as main.cpp and use clang++ -std=c++17 -Wall -Wextra -pedantic main.cpp -o school, or equivalent g++ flags. Recompile after editing so that you run the updated executable.

The source and output below belong to the edited program. Compare them with the original example to see the consequence of this single change.

C++17 · worked variation · complete program

#include <iostream>
#include <optional>

int main()  {
  std::optional<int> marks;
  std::cout << marks.value_or(0) << '\n';
  marks = 80;
  std::cout << *marks << '\n';
  return 0;
}

Expected output

0
80
Run in C++ compiler →

Check your understanding

C++17 · original program for this question

#include <iostream>
#include <optional>

int main()  {
  std::optional<int> marks;
  std::cout << marks.value_or(0) << '\n';
  marks = 72;
  std::cout << *marks << '\n';
  return 0;
}
Before making the practice edit, what does the original C++17 program print?
Choose one answer. You can try again.

Answer and explanation

0 72

Std::optional<T> represents either a T value or no value. It is useful for a student mark that has not yet been entered, avoiding a magic number masquerading as a real mark. Dereference optional only when it contains a value. Variant instead holds one of a fixed set of types; neither type substitutes for checking domain rules.

What to remember

Std::optional represents either a T value or no value. Dereference optional only when it contains a value. Variant instead holds one of a fixed set of types; neither type substitutes for checking domain rules.

Useful connections: Summarize and Convert Values, Give a Task a Name.

Language and design guidance: C++ Core Guidelines and C++ working draft. The draft describes an evolving language; these examples deliberately target C++17.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Handle a Recoverable Failure →