Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for a snapshot of the develop branch, built from commit d7c8a7cf0d.

libs/optional/doc/90_dependencies.qbk

[/
    Boost.Optional

    Copyright (c) 2003-2007 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)
]


[section Dependencies and Portability]

[section Dependencies]
The implementation uses the following other Boost modules:

# assert
# config
# core
# move
# mpl
# static_assert
# throw_exception
# type_traits
# utility

[endsect]

[section Emplace operations in older compilers][#optional_emplace_workaround]

Certain constructors and functions in the interface of `optional` perform a 'perfect forwarding' of arguments:

    template<class... Args> optional(in_place_init_t, Args&&... args);
    template<class... Args> optional(in_place_init_if_t, bool condition, Args&&... args);
    template<class... Args> void emplace(Args&&... args);

On compilers that do not support variadic templates, each of these functions is substituted with two overloads, one forwarding a single argument, the other forwarding zero arguments. This forms the following set:

    template<class Arg> optional(in_place_init_t, Arg&& arg);
    optional(in_place_init_t);

    template<class Arg> optional(in_place_init_if_t, bool condition, Arg&& arg);
    optional(in_place_init_if_t, bool condition);

    template<class Arg> void emplace(Arg&& arg);
    void emplace();

On compilers that do not support rvalue references, each of these functions is substituted with three overloads: taking `const` and non-`const` lvalue reference, and third forwarding zero arguments. This forms the following set:

    template<class Arg> optional(in_place_init_t, const Arg& arg);
    template<class Arg> optional(in_place_init_t, Arg& arg);
    optional(in_place_init_t);

    template<class Arg> optional(in_place_init_if_t, bool condition, const Arg& arg);
    template<class Arg> optional(in_place_init_if_t, bool condition, Arg& arg);
    optional(in_place_init_if_t, bool condition);

    template<class Arg> void emplace(const Arg& arg);
    template<class Arg> void emplace(Arg& arg);
    void emplace();

This workaround addresses about 40% of all use cases. If this is insufficient, you need to resort to using [link boost_optional.tutorial.in_place_factories In-Place Factories].
[endsect]

[section Optional Reference Binding][#optional_reference_binding]

A number of compilers incorrectly treat const lvalues of integral type as rvalues, and create an illegal temporary when binding to an lvalue reference to const in some expressions. This could result in creating an optional lvalue reference that is in fact bound to an unexpected temporary rather than to the intended object. In order to prevent hard to find run-time bugs, this library performs compile-time checks to prevent expressions that would otherwise bind an optional reference to an unexpected temporary. As a consequence, on certain compilers certain pieces of functionality in optional references are missing. In order to maintain a portability of your code across different compilers, it is recommended that you only stick to the minimum portable interface of optional references: prefer direct-initialization and copy assignment of optional references to copy-initialization and assignment from `T&`:

    const int i = 0;
    optional<const int&> or1;
    optional<const int&> or2 = i;  // caution: not portable
    or1 = i;                       // caution: not portable

    optional<const int&> or3(i);   // portable
    or1 = optional<const int&>(i); // portable

Compilers known to have these deficiencies include GCC versions 4.2, 4.3, 4.4, 4.5, 5.1, 5.2; QCC 4.4.2; MSVC versions 8.0, 9.0, 10.0, 11.0, 12.0. In order to check if your compiler correctly implements reference binding use this test program.

    #include <cassert>

    const int global_i = 0;

    struct TestingReferenceBinding
    {
      TestingReferenceBinding(const int& ii)
      {
        assert(&ii == &global_i);
      }

      void operator=(const int& ii)
      {
        assert(&ii == &global_i);
      }

      void operator=(int&&) // remove this if your compiler doesn't have rvalue refs
      {
        assert(false);
      }
    };

    int main()
    {
      const int& iref = global_i;
      assert(&iref == &global_i);

      TestingReferenceBinding ttt = global_i;
      ttt = global_i;

      TestingReferenceBinding ttt2 = iref;
      ttt2 = iref;
    }

[endsect]

[endsect]