This pattern is licensed under the CC0 Public Domain Dedication.
Allow argument values to be omitted when calling a function.
Note: This pattern uses experimental entities from the Library Fundamentals TS that demonstrate likely upcoming features in C++ but should not be used in production code.
foo, on lines 6–9, takes three arguments, two of which
the Library Fundamentals TS). This allows the value of those
arguments to be omitted, as shown on lines 14–16, where
represents no value.
This approach is more expressive than using pointers and
A related technique is the use of default arguments, which allow
arguments to be omitted entirely, but only from the end of the
If you are constructing an object with a complex combinatorial set of optional arguments, consider using the builder pattern.
std::experimental::nullopt are part of the Library Fundamentals
Technical Specification, which provides experimental features that
may soon be introduced to the C++ standard. They should not be used
in production code.