gcc/libstdc++-v3/testsuite/20_util/optional/make_optional.cc
Ville Voutilainen 25a69162c9 Implement P0032R3, Homogeneous interface for variant, any and optional,
for the parts concerning any and optional.
	* include/std/any (_Storage()): Make constexpr and have it
	initialize _M_ptr.
	(any()): Make constexpr.
	(any(const any&)): Adjust.
	(any(any&&)): Likewise.
	(__any_constructible_t): New.
	(any(_ValueType&&)): Constrain.
	(any(in_place_type_t<_Tp>, _Args&&...)): New.
	(any(in_place_type_t<_Tp>, initializer_list<_Up>, _Args&&...)):
	Likewise.
	(~any()): Adjust.
	(operator=(const any&)): Likewise.
	(operator=(any&&)): Likewise.
	(operator=(_ValueType&&)): Constrain.
	(emplace(_Args&&...)): New.
	(emplace(initializer_list<_Up>, _Args&&...)): Likewise.
	(clear()): Remove.
	(reset()): New.
	(swap(any&)): Adjust.
	(empty()): Remove.
	(has_value()): New.
	(type()): Adjust.
	(_Manager_internal::_S_create(_Storage&, _Args&&...)): New.
	(_Manager_external::_S_create(_Storage&, _Args&&...)): Likewise.
	(make_any(_Args&&...)): Likewise.
	(make_any(initializer_list<_Up>, _Args&&...)): Likewise.
	* include/std/optional (in_place_t, in_place): Remove.
	(bad_optional_access): Add a comment referring to LEWG 72.
	(emplace(_Args&&...)): Constrain.
	(has_value()): New.
	(reset()): Likewise.
	(make_optional(_Args&&...)): Likewise.
	(make_optional(initializer_list<_Up>, _Args&&...)): Likewise.
	* include/std/utility (in_place_tag): New.
	(__in_place, __in_place_type, __in_place_index): Likewise.
	(in_place_t, in_place_type_t, in_place_index_t): Likewise.
	(in_place(__in_place*)): Likewise.
	(in_place(__in_place_type<_Tp>*)): Likewise.
	(in_place(__in_place_index<_Idx>*)): Likewise.
	* testsuite/20_util/any/assign/1.cc: Adjust.
	* testsuite/20_util/any/assign/emplace.cc: New.
	* testsuite/20_util/any/assign/self.cc: Adjust.
	* testsuite/20_util/any/cons/1.cc: Likewise.
	* testsuite/20_util/any/cons/in_place.cc: New.
	* testsuite/20_util/any/make_any.cc: Likewise.
	* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
	* testsuite/20_util/any/misc/swap.cc: Likewise.
	* testsuite/20_util/any/modifiers/1.cc: Likewise.
	* testsuite/20_util/any/requirements.cc: New.
	* testsuite/20_util/in_place/requirements.cc: Likewise.
	* testsuite/20_util/optional/constexpr/in_place.cc: Adjust.
	* testsuite/20_util/optional/in_place.cc: Likewise.
	* testsuite/20_util/optional/make_optional.cc: Add tests for
	the new overloads of make_optional.

From-SVN: r238329
2016-07-14 12:10:05 +03:00

62 lines
2.2 KiB
C++

// { dg-options "-std=gnu++17" }
// { dg-do run }
// 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
// <http://www.gnu.org/licenses/>.
#include <optional>
#include <testsuite_hooks.h>
#include <vector>
#include <tuple>
struct combined {
std::vector<int> v;
std::tuple<int, int> t;
template<class... Args>
combined(std::initializer_list<int> il, Args&&... args)
: v(il), t(std::forward<Args>(args)...)
{
}
};
int main()
{
const int i = 42;
auto o = std::make_optional(i);
static_assert( std::is_same<decltype(o), std::optional<int>>(), "" );
VERIFY( o && *o == 42 );
VERIFY( &*o != &i );
auto o2 = std::make_optional<std::tuple<int, int>>(1, 2);
static_assert( std::is_same<decltype(o2),
std::optional<std::tuple<int, int>>>(), "" );
VERIFY( o2 && std::get<0>(*o2) == 1 && std::get<1>(*o2) == 2);
auto o3 = std::make_optional<std::vector<int>>({42, 666});
static_assert( std::is_same<decltype(o3),
std::optional<std::vector<int>>>(), "" );
VERIFY(o3 && (*o3)[0] == 42 && (*o3)[1] == 666);
auto o4 = std::make_optional<combined>({42, 666});
static_assert( std::is_same<decltype(o4),
std::optional<combined>>(), "" );
VERIFY(o4 && (o4->v)[0] == 42 && (o4->v)[1] == 666
&& std::get<0>(o4->t) == 0 && std::get<1>(o4->t) == 0 );
auto o5 = std::make_optional<combined>({1, 2}, 3, 4);
static_assert( std::is_same<decltype(o5),
std::optional<combined>>(), "" );
VERIFY(o4 && (o5->v)[0] == 1 && (o5->v)[1] == 2
&& std::get<0>(o5->t) == 3 && std::get<1>(o5->t) == 4 );
}