Accessing optional Values
Use value_or and has_value.
Accessing optional Values is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Dereference Operator
The fastest way to read an optional is *opt, but only when you are sure it holds a value, there is no check.
#include <iostream>
#include <optional>
int main() {
std::optional<int> o = 10;
if (o) std::cout << *o << "\n";
}The value() Method
opt.value() returns the value, but throws std::bad_optional_access if the optional is empty. Use it when an empty optional is a real error.
#include <iostream>
#include <optional>
int main() {
std::optional<int> o;
try { std::cout << o.value() << "\n"; }
catch (const std::bad_optional_access&) { std::cout << "empty!\n"; }
}value_or for Defaults
opt.value_or(fallback) returns the contained value if present, otherwise the fallback. No branching, no exceptions.
#include <iostream>
#include <optional>
int main() {
std::optional<int> o = std::nullopt;
std::cout << o.value_or(-1) << "\n"; // -1
}value_or with Real Data
A common use is supplying a default for missing configuration or lookups.
#include <iostream>
#include <optional>
#include <string>
std::optional<std::string> lookup(bool found) {
if (found) return std::string("Pro");
return std::nullopt;
}
int main() {
std::cout << lookup(false).value_or("Free") << "\n";
}Arrow Operator
For optionals of class types, opt->member accesses members directly (again assuming non-empty).
#include <iostream>
#include <optional>
#include <string>
int main() {
std::optional<std::string> o = std::string("hello");
if (o) std::cout << o->size() << "\n";
}Guard Before Access
The safe idiom is: check, then use. Combining the check and access avoids both exceptions and undefined behavior.
#include <iostream>
#include <optional>
int main() {
std::optional<int> o = 7;
if (o.has_value()) {
int x = *o;
std::cout << x * 2 << "\n";
}
}Init-Statement in if
You can declare and check in one line using an if init-statement, keeping the optional scoped to the branch.
#include <iostream>
#include <optional>
std::optional<int> find() { return 3; }
int main() {
if (auto v = find(); v.has_value())
std::cout << *v << "\n";
}Transform (C++23)
Monadic helpers like transform apply a function only if a value is present, returning a new optional. This chains computations cleanly.
and_then for Chaining
and_then (C++23) calls a function that itself returns an optional, flattening the result, ideal for steps that may each fail.
Choosing an Accessor
Use value_or when you have a sensible default, value() when emptiness is an error worth an exception, and */-> only after an explicit check.
Avoid Unchecked Dereference
Never call *opt without confirming the optional is non-empty. It is undefined behavior and a common source of crashes.
Quick Check
Check your understanding of optional access.
Recap
You learned how to access optionals:
*and->after a check.value()throws on empty.value_orsupplies a default.- Monadic
transform/and_thenchain operations (C++23).
Frequently asked questions
Is the “Accessing optional Values” lesson free?
Yes — the full text of “Accessing optional Values” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Accessing optional Values”?
Use value_or and has_value. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Accessing optional Values” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Representing Absence with optional
- Accessing optional Values
- std::expected Basics
- Error Handling Patterns