diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d888864600e..46f7f748f65 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,18 @@ +2016-07-06 Ville Voutilainen + + Implement LWG 2451, optional should 'forward' T's + implicit conversions. + * include/experimental/optional (__is_optional_impl, __is_optional): + New. + (optional()): Make constexpr and default. + (optional(_Up&&), optional(const optional<_Up>&), + optional(optional<_Up>&& __t): New. + (operator=(_Up&&)): Constrain. + (operator=(const optional<_Up>&), operator=(optional<_Up>&&)): New. + * testsuite/experimental/optional/cons/value.cc: + Add tests for the functionality added by LWG 2451. + * testsuite/experimental/optional/cons/value_neg.cc: New. + 2016-07-05 Ville Voutilainen Implement LWG 2509, diff --git a/libstdc++-v3/include/experimental/optional b/libstdc++-v3/include/experimental/optional index 7524a7e1357..b6425b7d00e 100644 --- a/libstdc++-v3/include/experimental/optional +++ b/libstdc++-v3/include/experimental/optional @@ -470,6 +470,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool _M_engaged = false; }; + template + class optional; + + template + struct __is_optional_impl : false_type + { }; + + template + struct __is_optional_impl> : true_type + { }; + + template + struct __is_optional + : public __is_optional_impl>> + { }; + + /** * @brief Class template for optional values. */ @@ -502,6 +519,78 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // _Optional_base has the responsibility for construction. using _Base::_Base; + constexpr optional() = default; + // Converting constructors for engaged optionals. + template >, + is_constructible<_Tp, _Up&&>, + is_convertible<_Up&&, _Tp> + >::value, bool> = true> + constexpr optional(_Up&& __t) + : _Base(_Tp(std::forward<_Up>(__t))) { } + + template >, + is_constructible<_Tp, _Up&&>, + __not_> + >::value, bool> = false> + explicit constexpr optional(_Up&& __t) + : _Base(_Tp(std::forward<_Up>(__t))) { } + + template >, + __not_&>>, + __not_&, _Tp>>, + is_constructible<_Tp, const _Up&>, + is_convertible + >::value, bool> = true> + constexpr optional(const optional<_Up>& __t) + : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { } + + template >, + __not_&>>, + __not_&, _Tp>>, + is_constructible<_Tp, const _Up&>, + __not_> + >::value, bool> = false> + explicit constexpr optional(const optional<_Up>& __t) + : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { } + + template >, + __not_&&>>, + __not_&&, _Tp>>, + is_constructible<_Tp, _Up&&>, + is_convertible<_Up&&, _Tp> + >::value, bool> = true> + constexpr optional(optional<_Up>&& __t) + : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { } + + template >, + __not_&&>>, + __not_&&, _Tp>>, + is_constructible<_Tp, _Up&&>, + __not_> + >::value, bool> = false> + explicit constexpr optional(optional<_Up>&& __t) + : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { } + // [X.Y.4.3] (partly) Assignment. optional& operator=(nullopt_t) noexcept @@ -510,8 +599,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } - template - enable_if_t>::value, optional&> + template>, + __not_<__is_optional<_Up>>>::value, + bool> = true> + optional& operator=(_Up&& __u) { static_assert(__and_, @@ -526,6 +619,57 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } + template>>::value, + bool> = true> + optional& + operator=(const optional<_Up>& __u) + { + static_assert(__and_, + is_assignable<_Tp&, _Up>>(), + "Cannot assign to value type from argument"); + + if (__u) + { + if (this->_M_is_engaged()) + this->_M_get() = *__u; + else + this->_M_construct(*__u); + } + else + { + this->_M_reset(); + } + return *this; + } + + template>>::value, + bool> = true> + optional& + operator=(optional<_Up>&& __u) + { + static_assert(__and_, + is_assignable<_Tp&, _Up>>(), + "Cannot assign to value type from argument"); + + if (__u) + { + if (this->_M_is_engaged()) + this->_M_get() = std::move(*__u); + else + this->_M_construct(std::move(*__u)); + } + else + { + this->_M_reset(); + } + + return *this; + } + template void emplace(_Args&&... __args) diff --git a/libstdc++-v3/testsuite/experimental/optional/cons/value.cc b/libstdc++-v3/testsuite/experimental/optional/cons/value.cc index a916951b874..123a89ede04 100644 --- a/libstdc++-v3/testsuite/experimental/optional/cons/value.cc +++ b/libstdc++-v3/testsuite/experimental/optional/cons/value.cc @@ -22,6 +22,7 @@ #include #include +#include struct tracker { @@ -236,4 +237,22 @@ int main() VERIFY( result == caught ); } + + { + std::experimental::optional os = "foo"; + struct X + { + explicit X(int) {} + X& operator=(int) {return *this;} + }; + std::experimental::optional ox{42}; + std::experimental::optional oi{42}; + std::experimental::optional ox2{oi}; + std::experimental::optional os2; + os2 = "foo"; + std::experimental::optional ox3; + ox3 = 42; + std::experimental::optional ox4; + ox4 = oi; + } } diff --git a/libstdc++-v3/testsuite/experimental/optional/cons/value_neg.cc b/libstdc++-v3/testsuite/experimental/optional/cons/value_neg.cc new file mode 100644 index 00000000000..c862a04986a --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/optional/cons/value_neg.cc @@ -0,0 +1,39 @@ +// { dg-options "-std=gnu++14" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +#include +#include + +int main() +{ + { + struct X + { + explicit X(int) {} + }; + std::experimental::optional ox{42}; + std::experimental::optional ox2 = 42; // { dg-error "conversion" } + std::experimental::optional> oup{new int}; + std::experimental::optional> oup2 = new int; // { dg-error "conversion" } + } +}