The C++17 standard came with a lot of great features and std::optional
was one of the main features of today’s modern C++. std::optional<T>
is a class template that is defined in the <optional>
header and represents either a T
value or no value. In this post, we explain, what is optional in modern C++ and how we can use it efficiently.
What is the optional class template in C++ 17 and beyond?
The std::optional
feature is a class template that is defined in the <optional>
header and represents either a T
value or no value (which is signified by the tag type nullopt_t
). In some respects, this can be thought of as equivalent to variant, but with a purpose-built interface.
Here is the definition of the std::optional
class template.
1 2 3 |
template< class T > class optional; |
Optional
can be used to define any type of variables as below.
1 2 3 4 |
std::optional<int> a(5); std::optional<int> b; |
An optional variable can be checked by has_value()
method if it has a value or not.
1 2 3 |
if (a.has_value()) { } |
Here is another example that has a function with an optional return.
1 2 3 4 5 6 |
std::optional<std::string> testopt(std::string s) { if(s.length()==0) return {}; else return s; } |
as you see our function may return a string as an option or it may have no return value.
A common use case for optional
is the return value of a function that may fail. Any instance of optional<T>
at any given point in time either contains a value or does not contain a value. If an optional<T>
contains a value, the value is guaranteed to be allocated as part of the optional
object footprint, i.e. no dynamic memory allocation ever takes place. Thus, an optional
object models an object, not a pointer, even though operator*() and operator->() are defined.
Is there a simple example about the optional class template in C++ 17?
Here is a simple example about the std::optional
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <optional> int main() { std::optional<int> a(5); // a = 5 std::optional<int> b; // b has no value if (a.has_value()) { int z = a.value() + b.value_or(0); std::cout << z; } system("pause"); return 0; } |
Is there a full example about the optional class template in C++ 17?
Here is a full example that we can use optional return
for functions. Let’s create a function that may return a string as an option or no return. We can obtain optional value by .value
or by .value_or()
method if no value. The C++ Builder application example below explains all.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <optional> #include <string> std::optional<std::string> testopt(std::string s) { if(s.length()==0) return {}; else return s; } int main() { std::string str; str = testopt("").value_or("- no return -"); std::cout << str << std::endl; str = testopt("testing optional").value(); std::cout << str << std::endl; system("pause"); return 0; } |
and here is the output:
1 2 3 4 |
- no return - testing optional |
The std::optional
class template has swap()
, reset()
, emplace()
modifiers as well as ->
, *
, bool
, has_value()
, value()
, value_or()
observers. It has non-member functions to compare optional objects such as =
, ==
, !=
, <
, >, <=
, >=
, operators in C++17 and <=>
operator in C++20 . In C++23, It has and_then()
, or_else()
, transform()
monadic operations. The std::optional has helpers and helper classes too.
For more details about this feature in C++17 standard, please see these papers; P0220R1, P0307R2, P0032R3, P0504R0
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition