Boost C++ Libraries Home Libraries People FAQ More

Next

Chapter 1. Boost.Optional

Fernando Luis Cacciola Carballal

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Table of Contents

Introduction
Motivation
Development
The models
The semantics
The Interface
Synopsis
Detailed Semantics
Examples
Optional return values
Optional local variables
Optional data members
Bypassing expensive unnecessary default construction
Optional references
Rebinding semantics for assignment of optional references
In-Place Factories
A note about optional<bool>
Exception Safety Guarantees
Type requirements
Dependencies and Portability
Optional Reference Binding
Acknowledgments

This library can be used to represent 'optional' (or 'nullable') objects that can be safely passed by value:

optional<int> readInt(); // this function may return either an int or a not-an-int

if (optional<int> oi = readInt()) // did I get a real int
  cout << "my int is: " << *oi;   // use my int
else
  cout << "I have no int";

Last revised: May 23, 2014 at 14:35:08 GMT


Next