C++C++17C++20Learn C++

What Is The New Optional Class Template In C++ 17?

What Is The New Optional Class Template In C++ 17

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.

Optional can be used to define any type of variables as below.

An optional variable can be checked by has_value() method if it has a value or not.

Here is another example that has a function with an optional return.

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,

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.

and here is the output:

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; P0220R1P0307R2P0032R3P0504R0

What Is The New Optional Class Template In C++ 17 C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?