Eric Fiselier a9e659619f Implement N4606 optional
Summary:
Adapt implementation of Library Fundamentals TS optional into an implementation of N4606 optional.

  - Update relational operators per http://wg21.link/P0307
  - Update to requirements of http://wg21.link/P0032
  - Extension: Implement trivial copy/move construction/assignment for `optional<T>` when `T` is trivially copyable.

Audit P/Rs for optional LWG issues:
  - 2756 "C++ WP optional<T> should 'forward' T's implicit conversions" Implemented, which also resolves 2753 "Optional's constructors and assignments need constraints" (modulo my refusal to explicitly delete the move operations, which is a design error that I'm working on correcting in the 2756 P/R).
  - 2736 "nullopt_t insufficiently constrained" Already conforming. I've added a test ensuring that `nullopt_t` is not copy-initializable from an empty braced-init-list, which I believe is the root intent of the issue, to avoid regression.
  - 2740 "constexpr optional<T>::operator->" Already conforming.
  - 2746 "Inconsistency between requirements for emplace between optional and variant" No P/R, but note that the author's '"suggested resolution" is already implemented.
  - 2748 "swappable traits for optionals" Already conforming.
  - 2753 "Optional's constructors and assignments need constraints" Implemented.

Most of the work for this patch was done by Casey Carter @ Microsoft. Thank you Casey!



Reviewers: mclow.lists, CaseyCarter, EricWF

Differential Revision: https://reviews.llvm.org/D22741

llvm-svn: 283980
2016-10-12 07:46:20 +00:00

151 lines
3.6 KiB
C++

//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03, c++11, c++14
// <optional>
// optional(const optional<T>& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.hpp"
using std::optional;
template <class T, class ...InitArgs>
void test(InitArgs&&... args)
{
const optional<T> rhs(std::forward<InitArgs>(args)...);
bool rhs_engaged = static_cast<bool>(rhs);
optional<T> lhs = rhs;
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(*lhs == *rhs);
}
void test_throwing_ctor() {
#ifndef TEST_HAS_NO_EXCEPTIONS
struct Z {
Z() : count(0) {}
Z(Z const& o) : count(o.count + 1)
{ if (count == 2) throw 6; }
int count;
};
const Z z;
const optional<Z> rhs(z);
try
{
optional<Z> lhs(rhs);
assert(false);
}
catch (int i)
{
assert(i == 6);
}
#endif
}
template <class T, class ...InitArgs>
void test_ref(InitArgs&&... args)
{
const optional<T> rhs(std::forward<InitArgs>(args)...);
bool rhs_engaged = static_cast<bool>(rhs);
optional<T> lhs = rhs;
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(&(*lhs) == &(*rhs));
}
void test_reference_extension()
{
#if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
using T = TestTypes::TestType;
T::reset();
{
T t;
T::reset_constructors();
test_ref<T&>();
test_ref<T&>(t);
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::destroyed == 1);
assert(T::alive == 0);
{
T t;
const T& ct = t;
T::reset_constructors();
test_ref<T const&>();
test_ref<T const&>(t);
test_ref<T const&>(ct);
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::alive == 0);
assert(T::destroyed == 1);
{
static_assert(!std::is_copy_constructible<std::optional<T&&>>::value, "");
static_assert(!std::is_copy_constructible<std::optional<T const&&>>::value, "");
}
#endif
}
int main()
{
test<int>();
test<int>(3);
{
using T = TestTypes::TestType;
T::reset();
const optional<T> rhs;
assert(T::alive == 0);
const optional<T> lhs(rhs);
assert(lhs.has_value() == false);
assert(T::alive == 0);
}
TestTypes::TestType::reset();
{
using T = TestTypes::TestType;
T::reset();
const optional<T> rhs(42);
assert(T::alive == 1);
assert(T::value_constructed == 1);
assert(T::copy_constructed == 0);
const optional<T> lhs(rhs);
assert(lhs.has_value());
assert(T::copy_constructed == 1);
assert(T::alive == 2);
}
TestTypes::TestType::reset();
{
using namespace ConstexprTestTypes;
test<TestType>();
test<TestType>(42);
}
{
using namespace TrivialTestTypes;
test<TestType>();
test<TestType>(42);
}
{
test_throwing_ctor();
}
{
test_reference_extension();
}
}