Search code examples
c++option-typenoexceptboost-optional

Can an uninitialised std::optional or boost::optional constructor throw?


Can either of the following template methods be declared noexcept?

template <typename T>
std::optional<T> foo(const T& value) // noexcept?
{
    try {
        // code possibly returning a T or a std::nullopt
    } catch(...) {
        return std::nullopt;
    }
}

template <typename T>
boost::optional<T> bar(const T& value) // noexcept?
{
    try {
        // code possibly returning a T or a boost::none
    } catch(...) {
        return boost::none;
    }
}

In other words, can an uninitialised std/boost::optional (nullopt/none) throw?


Solution

  • Default constructor of optional is declared noexcept, as per http://en.cppreference.com/w/cpp/experimental/optional/optional.