From 197c757cb11681a6ff6df1491ebfde4f5a392627 Mon Sep 17 00:00:00 2001 From: Tim Shen Date: Thu, 18 Aug 2016 20:31:26 +0000 Subject: [PATCH] Implement * include/Makefile.am: Add new file std/variant. * include/Makefile.in: Generated from Makefile.am. * include/bits/enable_special_members.h: Add a tag type to allow the construction in non-default constructor. * include/bits/uses_allocator.h: Add convenience traits to detect constructibility. * include/std/variant: Implement . * testsuite/20_util/variant/compile.cc: Compile-time tests. * testsuite/20_util/variant/run.cc: Runtime tests. From-SVN: r239590 --- libstdc++-v3/ChangeLog | 14 + libstdc++-v3/include/Makefile.am | 1 + libstdc++-v3/include/Makefile.in | 1 + .../include/bits/enable_special_members.h | 37 +- libstdc++-v3/include/bits/uses_allocator.h | 51 + libstdc++-v3/include/std/variant | 1360 +++++++++++++++++ .../testsuite/20_util/variant/compile.cc | 405 +++++ libstdc++-v3/testsuite/20_util/variant/run.cc | 501 ++++++ 8 files changed, 2368 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/include/std/variant create mode 100644 libstdc++-v3/testsuite/20_util/variant/compile.cc create mode 100644 libstdc++-v3/testsuite/20_util/variant/run.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index b6a0965af4a..a2fa63e6762 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,17 @@ +2016-08-18 Tim Shen + + Implement + + * include/Makefile.am: Add new file std/variant. + * include/Makefile.in: Generated from Makefile.am. + * include/bits/enable_special_members.h: Add a tag type to allow + the construction in non-default constructor. + * include/bits/uses_allocator.h: Add convenience traits to + detect constructibility. + * include/std/variant: Implement . + * testsuite/20_util/variant/compile.cc: Compile-time tests. + * testsuite/20_util/variant/run.cc: Runtime tests. + 2016-08-18 Jonathan Wakely * doc/xml/manual/test.xml (test.run.permutations): Expand section. diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index ea992f05319..0a10994de4e 100644 --- a/libstdc++-v3/include/Makefile.am +++ b/libstdc++-v3/include/Makefile.am @@ -77,6 +77,7 @@ std_headers = \ ${std_srcdir}/unordered_set \ ${std_srcdir}/utility \ ${std_srcdir}/valarray \ + ${std_srcdir}/variant \ ${std_srcdir}/vector bits_srcdir = ${glibcxx_srcdir}/include/bits diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Makefile.in index b30eeae5f49..8b774a98f7b 100644 --- a/libstdc++-v3/include/Makefile.in +++ b/libstdc++-v3/include/Makefile.in @@ -367,6 +367,7 @@ std_headers = \ ${std_srcdir}/unordered_set \ ${std_srcdir}/utility \ ${std_srcdir}/valarray \ + ${std_srcdir}/variant \ ${std_srcdir}/vector bits_srcdir = ${glibcxx_srcdir}/include/bits diff --git a/libstdc++-v3/include/bits/enable_special_members.h b/libstdc++-v3/include/bits/enable_special_members.h index 1ac8f38935d..07c6c99ef85 100644 --- a/libstdc++-v3/include/bits/enable_special_members.h +++ b/libstdc++-v3/include/bits/enable_special_members.h @@ -36,13 +36,33 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION + struct _Enable_default_constructor_tag + { + explicit _Enable_default_constructor_tag() = default; + }; + /** * @brief A mixin helper to conditionally enable or disable the default * constructor. * @sa _Enable_special_members */ template - struct _Enable_default_constructor { }; + struct _Enable_default_constructor + { + constexpr _Enable_default_constructor() noexcept = default; + constexpr _Enable_default_constructor(_Enable_default_constructor const&) + noexcept = default; + constexpr _Enable_default_constructor(_Enable_default_constructor&&) + noexcept = default; + _Enable_default_constructor& + operator=(_Enable_default_constructor const&) noexcept = default; + _Enable_default_constructor& + operator=(_Enable_default_constructor&&) noexcept = default; + + // Can be used in other ctors. + constexpr explicit + _Enable_default_constructor(_Enable_default_constructor_tag) { } + }; /** @@ -86,7 +106,20 @@ template struct _Enable_default_constructor - { constexpr _Enable_default_constructor() noexcept = delete; }; + { + constexpr _Enable_default_constructor() noexcept = delete; + constexpr _Enable_default_constructor(_Enable_default_constructor const&) + noexcept = default; + constexpr _Enable_default_constructor(_Enable_default_constructor&&) + noexcept = default; + _Enable_default_constructor& + operator=(_Enable_default_constructor const&) noexcept = default; + _Enable_default_constructor& + operator=(_Enable_default_constructor&&) noexcept = default; + + // Can be used in other ctors. + explicit _Enable_default_constructor(_Enable_default_constructor_tag) { } + }; template struct _Enable_destructor diff --git a/libstdc++-v3/include/bits/uses_allocator.h b/libstdc++-v3/include/bits/uses_allocator.h index 46aea1327a6..500bd901554 100644 --- a/libstdc++-v3/include/bits/uses_allocator.h +++ b/libstdc++-v3/include/bits/uses_allocator.h @@ -113,6 +113,57 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr bool uses_allocator_v = uses_allocator<_Tp, _Alloc>::value; #endif // C++17 + template class _Predicate, + typename _Tp, typename _Alloc, typename... _Args> + struct __is_uses_allocator_predicate + : conditional::value, + __or_<_Predicate<_Tp, allocator_arg_t, _Alloc, _Args...>, + _Predicate<_Tp, _Args..., _Alloc>>, + _Predicate<_Tp, _Args...>>::type { }; + + template + struct __is_uses_allocator_constructible + : __is_uses_allocator_predicate + { }; + + template + constexpr bool __is_uses_allocator_constructible_v = + __is_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value; + + template + struct __is_nothrow_uses_allocator_constructible + : __is_uses_allocator_predicate + { }; + + + template + constexpr bool __is_nothrow_uses_allocator_constructible_v = + __is_nothrow_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value; + + template + void __uses_allocator_construct_impl(__uses_alloc0 __a, _Tp* __ptr, + _Args&&... __args) + { new (__ptr) _Tp(forward<_Args>(__args)...); } + + template + void __uses_allocator_construct_impl(__uses_alloc1<_Alloc> __a, _Tp* __ptr, + _Args&&... __args) + { new (__ptr) _Tp(allocator_arg, *__a._M_a, forward<_Args>(__args)...); } + + template + void __uses_allocator_construct_impl(__uses_alloc2<_Alloc> __a, _Tp* __ptr, + _Args&&... __args) + { new (__ptr) _Tp(forward<_Args>(__args)..., *__a._M_a); } + + template + void __uses_allocator_construct(const _Alloc& __a, _Tp* __ptr, + _Args&&... __args) + { + __uses_allocator_construct_impl(__use_alloc<_Tp, _Alloc, _Args...>(__a), + __ptr, forward<_Args>(__args)...); + } + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant new file mode 100644 index 00000000000..a9b43944770 --- /dev/null +++ b/libstdc++-v3/include/std/variant @@ -0,0 +1,1360 @@ +// -*- C++ -*- + +// Copyright (C) 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. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// . + +/** @file variant + * This is the C++ Library header. + */ + +#ifndef _GLIBCXX_VARIANT +#define _GLIBCXX_VARIANT 1 + +#pragma GCC system_header + +#if __cplusplus <= 201402L +# include +#else + +#include +#include +#include +#include + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + + template class tuple; + template class variant; + template struct hash; + + template + struct variant_size; + + template + struct variant_size : variant_size<_Variant> {}; + + template + struct variant_size : variant_size<_Variant> {}; + + template + struct variant_size : variant_size<_Variant> {}; + + template + struct variant_size> + : std::integral_constant {}; + + template + constexpr size_t variant_size_v = variant_size<_Variant>::value; + + template + struct variant_alternative; + + template + struct variant_alternative<_Np, variant<_First, _Rest...>> + : variant_alternative<_Np-1, variant<_Rest...>> {}; + + template + struct variant_alternative<0, variant<_First, _Rest...>> + { using type = _First; }; + + template + using variant_alternative_t = + typename variant_alternative<_Np, _Variant>::type; + + constexpr size_t variant_npos = -1; + +namespace __detail +{ +namespace __variant +{ + // Returns the first apparence of _Tp in _Types. + // Returns sizeof...(_Types) if _Tp is not in _Types. + template + struct __index_of : std::integral_constant {}; + + template + constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value; + + template + struct __index_of<_Tp, _First, _Rest...> : + std::integral_constant + ? 0 : __index_of_v<_Tp, _Rest...> + 1> {}; + + // Extract _From's qualifiers and references and apply it to _To. + // __reserved_type_map is const char&. + template + struct __reserved_type_map_impl + { using type = _To; }; + + template + using __reserved_type_map = + typename __reserved_type_map_impl<_From, _To>::type; + + template + struct __reserved_type_map_impl<_From&, _To> + { using type = add_lvalue_reference_t<__reserved_type_map<_From, _To>>; }; + + template + struct __reserved_type_map_impl<_From&&, _To> + { using type = add_rvalue_reference_t<__reserved_type_map<_From, _To>>; }; + + template + struct __reserved_type_map_impl + { using type = add_const_t<__reserved_type_map<_From, _To>>; }; + + template + struct __reserved_type_map_impl + { using type = add_volatile_t<__reserved_type_map<_From, _To>>; }; + + template + struct __reserved_type_map_impl + { using type = add_cv_t<__reserved_type_map<_From, _To>>; }; + + // Stores a reference alternative as a... well, reference. + template + struct _Reference_storage + { + static_assert(is_reference_v<_Reference>, + "BUG: _Reference should be a reference"); + + _Reference_storage(_Reference __ref) noexcept : _M_storage(__ref) { } + + operator _Reference() noexcept + { return static_cast<_Reference>(_M_storage); } + + _Reference _M_storage; + }; + + // Stores a void alternative, because it is not a regular type. + template + struct _Void_storage { }; + + // Map from the alternative type to a non-qualified storage type. + template + struct __storage_type + { using type = _Alternative; }; + + template + struct __storage_type<_Alternative, + enable_if_t>> + { using type = _Reference_storage<_Alternative>; }; + + template + struct __storage_type<_Alternative, enable_if_t>> + { using type = _Void_storage<_Alternative>; }; + + template + using __storage = typename __storage_type<_Type>::type; + + template> + struct _Uninitialized; + + template + struct _Uninitialized<_Type, true> + { + constexpr _Uninitialized() = default; + + template + constexpr _Uninitialized(in_place_index_t<0>, _Args&&... __args) + : _M_storage(std::forward<_Args>(__args)...) + { } + + _Type _M_storage; + }; + + template + struct _Uninitialized<_Type, false> + { + constexpr _Uninitialized() = default; + + template + constexpr _Uninitialized(in_place_index_t<0>, _Args&&... __args) + { ::new (&_M_storage) _Type(std::forward<_Args>(__args)...); } + + typename std::aligned_storage::type + _M_storage; + }; + + // Reverse mapping of __storage_type. + template + struct __alternative_type + { + static_assert(!is_reference_v<_Storage_type>, + "BUG: _Storage_type should not be reference"); + using type = _Storage_type; + }; + + template + struct __alternative_type<_Reference_storage<_Reference>> + { using type = _Reference; }; + + template + struct __alternative_type<_Void_storage<_Void>> + { using type = _Void; }; + + // Given a qualified storage type, return the desired reference. + // The qualified storage type is supposed to carry the variant object's + // qualifications and reference information, and the designated alternative's + // storage type. + // Returns the qualification-collapsed alternative references. + // + // For example, __get_alternative<_Reference_storage&> returns int&. + template + decltype(auto) + __get_alternative(void* __ptr) + { + using _Storage = decay_t<_Qualified_storage>; + using _Alternative = typename __alternative_type<_Storage>::type; + return __reserved_type_map<_Qualified_storage, _Alternative>( + *static_cast<_Storage*>(__ptr)); + } + + // Various functions as "vtable" entries, where those vtables are used by + // polymorphic operations. + template + constexpr void + __erased_ctor(void* __lhs, void* __rhs) + { ::new (__lhs) decay_t<_Lhs>(__get_alternative<_Rhs>(__rhs)); } + + template + constexpr void + __erased_use_alloc_ctor(const _Alloc& __a, void* __lhs, void* __rhs) + { + __uses_allocator_construct(__a, static_cast*>(__lhs), + __get_alternative<_Rhs>(__rhs)); + } + + // TODO: Find a potential chance to reuse this accross the project. + template + constexpr void + __erased_dtor(void* __ptr) + { + using _Storage = decay_t<_Tp>; + static_cast<_Storage*>(__ptr)->~_Storage(); + } + + template + constexpr void + __erased_assign(void* __lhs, void* __rhs) + { __get_alternative<_Lhs>(__lhs) = __get_alternative<_Rhs>(__rhs); } + + template + constexpr void + __erased_swap(void* __lhs, void* __rhs) + { + using std::swap; + swap(__get_alternative<_Lhs>(__lhs), __get_alternative<_Rhs>(__rhs)); + } + + template + constexpr bool + __erased_equal_to(void* __lhs, void* __rhs) + { return __get_alternative<_Lhs>(__lhs) == __get_alternative<_Rhs>(__rhs); } + + template + constexpr bool + __erased_less_than(void* __lhs, void* __rhs) + { return __get_alternative<_Lhs>(__lhs) < __get_alternative<_Rhs>(__rhs); } + + template + constexpr size_t + __erased_hash(void* __t) + { return std::hash>{}(__get_alternative<_Tp>(__t)); } + + template + struct _Variant_base; + + template + struct _Variant_storage + { constexpr _Variant_storage() = default; }; + + // Use recursive unions to implement a trivially destructible variant. + template + struct _Variant_storage<_First, _Rest...> + { + constexpr _Variant_storage() = default; + + template + constexpr _Variant_storage(in_place_index_t<0>, _Args&&... __args) + : _M_first(in_place<0>, forward<_Args>(__args)...) + { } + + template> + constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args) + : _M_rest(in_place<_Np - 1>, forward<_Args>(__args)...) + { } + + ~_Variant_storage() = default; + + constexpr void* + _M_storage() const + { + return const_cast( + static_cast(&_M_first._M_storage)); + } + + union + { + _Uninitialized<__storage<_First>> _M_first; + _Variant_storage<_Rest...> _M_rest; + }; + }; + + template + struct _Dtor_mixin + { + ~_Dtor_mixin() + { static_cast<_Derived*>(this)->_M_destroy(); } + }; + + template + struct _Dtor_mixin<_Derived, true> + { + ~_Dtor_mixin() = default; + }; + + // Helps SFINAE on special member functions. Otherwise it can live in variant + // class. + template + struct _Variant_base : + _Variant_storage<_Types...>, + _Dtor_mixin<_Variant_base<_Types...>, + __and_...>::value> + { + using _Storage = _Variant_storage<_Types...>; + + constexpr + _Variant_base() + noexcept(is_nothrow_default_constructible_v< + variant_alternative_t<0, variant<_Types...>>>) + : _Variant_base(in_place<0>) { } + + _Variant_base(const _Variant_base& __rhs) + : _Storage(), _M_index(__rhs._M_index) + { + if (__rhs._M_valid()) + { + static constexpr void (*_S_vtable[])(void*, void*) = + { &__erased_ctor<__storage<_Types>&, + const __storage<_Types>&>... }; + _S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage()); + } + } + + _Variant_base(_Variant_base&& __rhs) + noexcept(__and_...>::value) + : _Storage(), _M_index(__rhs._M_index) + { + if (__rhs._M_valid()) + { + static constexpr void (*_S_vtable[])(void*, void*) = + { &__erased_ctor<__storage<_Types>&, __storage<_Types>&&>... }; + _S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage()); + } + } + + template + constexpr explicit + _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args) + : _Storage(__i, forward<_Args>(__args)...), _M_index(_Np) + { } + + template + _Variant_base(const _Alloc& __a, const _Variant_base& __rhs) + : _Storage(), _M_index(__rhs._M_index) + { + if (__rhs._M_valid()) + { + static constexpr void + (*_S_vtable[])(const _Alloc&, void*, void*) = + { &__erased_use_alloc_ctor<_Alloc, __storage<_Types>&, + const __storage<_Types>&>... }; + _S_vtable[__rhs._M_index](__a, _M_storage(), __rhs._M_storage()); + } + } + + template + _Variant_base(const _Alloc& __a, _Variant_base&& __rhs) + : _Storage(), _M_index(__rhs._M_index) + { + if (__rhs._M_valid()) + { + static constexpr void + (*_S_vtable[])(const _Alloc&, void*, void*) = + { &__erased_use_alloc_ctor<_Alloc, __storage<_Types>&, + __storage<_Types>&&>... }; + _S_vtable[__rhs._M_index](__a, _M_storage(), __rhs._M_storage()); + } + } + + template + constexpr explicit + _Variant_base(const _Alloc& __a, in_place_index_t<_Np>, + _Args&&... __args) + : _Storage(), _M_index(_Np) + { + using _Storage = + __storage>>; + __uses_allocator_construct(__a, static_cast<_Storage*>(_M_storage()), + forward<_Args>(__args)...); + __glibcxx_assert(_M_index == _Np); + } + + _Variant_base& + operator=(const _Variant_base& __rhs) + { + if (_M_index == __rhs._M_index) + { + if (__rhs._M_valid()) + { + static constexpr void (*_S_vtable[])(void*, void*) = + { &__erased_assign<__storage<_Types>&, + const __storage<_Types>&>... }; + _S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage()); + } + } + else + { + _Variant_base __tmp(__rhs); + this->~_Variant_base(); + __try + { + ::new (this) _Variant_base(std::move(__tmp)); + } + __catch (...) + { + _M_index = variant_npos; + __throw_exception_again; + } + } + __glibcxx_assert(_M_index == __rhs._M_index); + return *this; + } + + _Variant_base& + operator=(_Variant_base&& __rhs) + noexcept(__and_..., + is_nothrow_move_assignable<_Types>...>::value) + { + if (_M_index == __rhs._M_index) + { + if (__rhs._M_valid()) + { + static constexpr void (*_S_vtable[])(void*, void*) = + { &__erased_assign<__storage<_Types>&, + __storage<_Types>&&>... }; + _S_vtable[__rhs._M_index](_M_storage(), __rhs._M_storage()); + } + } + else + { + this->~_Variant_base(); + __try + { + ::new (this) _Variant_base(std::move(__rhs)); + } + __catch (...) + { + _M_index = variant_npos; + __throw_exception_again; + } + } + return *this; + } + + void _M_destroy() + { + if (_M_valid()) + { + static constexpr void (*_S_vtable[])(void*) = + { &__erased_dtor<__storage<_Types>&>... }; + _S_vtable[this->_M_index](_M_storage()); + } + } + + constexpr void* + _M_storage() const + { return _Storage::_M_storage(); } + + constexpr bool + _M_valid() const noexcept + { return _M_index != variant_npos; } + + size_t _M_index; + }; + + // For how many times does _Tp appear in _Tuple? + template + struct __tuple_count; + + template + constexpr size_t __tuple_count_v = __tuple_count<_Tp, _Tuple>::value; + + template + struct __tuple_count<_Tp, tuple<_Types...>> + : integral_constant { }; + + template + struct __tuple_count<_Tp, tuple<_First, _Rest...>> + : integral_constant< + size_t, + __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { }; + + // TODO: Reuse this in ? + template + constexpr bool __exactly_once = __tuple_count_v<_Tp, tuple<_Types...>> == 1; + + // Takes _Types and create an overloaded _S_fun for each type. + // If a type appears more than once in _Types, create only one overload. + template + struct __overload_set + { static void _S_fun(); }; + + template + struct __overload_set<_First, _Rest...> : __overload_set<_Rest...> + { + using __overload_set<_Rest...>::_S_fun; + static integral_constant _S_fun(_First); + }; + + template + struct __overload_set : __overload_set<_Rest...> + { + using __overload_set<_Rest...>::_S_fun; + }; + + // Helper for variant(_Tp&&) and variant::operator=(_Tp&&). + // __accepted_index maps the arbitrary _Tp to an alternative type in _Variant. + template + struct __accepted_index + { static constexpr size_t value = variant_npos; }; + + template + struct __accepted_index< + _Tp, variant<_Types...>, + decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()), + std::declval())> + { + static constexpr size_t value = sizeof...(_Types) - 1 + - decltype(__overload_set<_Types...>:: + _S_fun(std::declval<_Tp>()))::value; + }; + + // Returns the raw storage for __v. + template + void* __get_storage(_Variant&& __v) + { return __v._M_storage(); } + + // Returns the reference to the desired alternative. + // It is as unsafe as a reinterpret_cast. + template + decltype(auto) __access(_Variant&& __v) + { + return __get_alternative<__reserved_type_map<_Variant&&, __storage<_Tp>>>( + __get_storage(forward<_Variant>(__v))); + } + + // A helper used to create variadic number of _To types. + template + using _To_type = _To; + + // Call the actual visitor. + // _Args are qualified storage types. + template + decltype(auto) __visit_invoke(_Visitor&& __visitor, + _To_type<_Args, void*>... __ptrs) + { + return forward<_Visitor>(__visitor)(__get_alternative<_Args>(__ptrs)...); + } + + // Used for storing multi-dimensional vtable. + template + struct _Multi_array + { + constexpr const _Tp& + _M_access() const + { return _M_data; } + + _Tp _M_data; + }; + + template + struct _Multi_array<_Tp, __first, __rest...> + { + template + constexpr const _Tp& + _M_access(size_t __first_index, _Args... __rest_indices) const + { return _M_arr[__first_index]._M_access(__rest_indices...); } + + _Multi_array<_Tp, __rest...> _M_arr[__first]; + }; + + // Creates a multi-dimensional vtable recursively. + // _Variant_tuple is initially the input from visit(), and gets gradually + // consumed. + // _Arg_tuple is enumerated alternative sequence, represented by a + // qualified storage. + // + // For example, + // visit([](auto, auto){}, + // variant(), + // variant()) + // will trigger instantiations of: + // __gen_vtable_impl<_Multi_array, + // tuple, + // variant>, + // tuple<>> + // __gen_vtable_impl<_Multi_array, + // tuple>, + // tuple> + // __gen_vtable_impl<_Multi_array, + // tuple<>, + // tuple> + // __gen_vtable_impl<_Multi_array, + // tuple<>, + // tuple> + // __gen_vtable_impl<_Multi_array, + // tuple<>, + // tuple> + // __gen_vtable_impl<_Multi_array, + // tuple>, + // tuple> + // __gen_vtable_impl<_Multi_array, + // tuple<>, + // tuple> + // __gen_vtable_impl<_Multi_array, + // tuple<>, + // tuple> + // __gen_vtable_impl<_Multi_array, + // tuple<>, + // tuple> + // The returned multi-dimensional vtable can be fast accessed by the visitor + // using index calculation. + template + struct __gen_vtable_impl; + + template + struct __gen_vtable_impl<_Array_type, tuple<_First, _Rest...>, + tuple<_Args...>> + { + static constexpr _Array_type + _S_apply() + { + _Array_type __vtable{}; + _S_apply_all_alts( + __vtable, make_index_sequence>>()); + return __vtable; + } + + template + static constexpr void + _S_apply_all_alts(_Array_type& __vtable, index_sequence<__indices...>) + { (_S_apply_single_alt<__indices>(__vtable._M_arr[__indices]), ...); } + + template + static constexpr void + _S_apply_single_alt(auto& __element) + { + using _Alternative = variant_alternative_t<__index, decay_t<_First>>; + using _Qualified_storage = __reserved_type_map< + _First, __storage<_Alternative>>; + __element = __gen_vtable_impl< + decay_t, tuple<_Rest...>, + tuple<_Args..., _Qualified_storage>>::_S_apply(); + } + }; + + template + struct __gen_vtable_impl< + _Multi_array<_Result_type (*)(_Visitor, _To_type<_Args, void*>...)>, + tuple<>, tuple<_Args...>> + { + using _Array_type = + _Multi_array<_Result_type (*)(_Visitor&&, _To_type<_Args, void*>...)>; + + static constexpr auto + _S_apply() + { return _Array_type{&__visit_invoke<_Visitor, _Args...>}; } + }; + + template + struct __gen_vtable + { + using _Func_ptr = + _Result_type (*)(_Visitor&&, _To_type<_Variants, void*>...); + using _Array_type = + _Multi_array<_Func_ptr, variant_size_v>...>; + + static constexpr _Array_type + _S_apply() + { + return __gen_vtable_impl< + _Array_type, tuple<_Variants...>, tuple<>>::_S_apply(); + } + }; + +} // namespace __variant +} // namespace __detail + + template + inline constexpr bool holds_alternative(const variant<_Types...>& __v) + noexcept + { + static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, + "T should occur for exactly once in alternatives"); + return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>; + } + + template + variant_alternative_t<_Np, variant<_Types...>>& + get(variant<_Types...>&); + + template + variant_alternative_t<_Np, variant<_Types...>>&& + get(variant<_Types...>&&); + + template + variant_alternative_t<_Np, variant<_Types...>> const& + get(const variant<_Types...>&); + + template + variant_alternative_t<_Np, variant<_Types...>> const&& + get(const variant<_Types...>&&); + + template + inline _Tp& get(variant<_Types...>& __v) + { + static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, + "T should occur for exactly once in alternatives"); + static_assert(!is_void_v<_Tp>, "_Tp should not be void"); + return get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v); + } + + template + inline _Tp&& get(variant<_Types...>&& __v) + { + static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, + "T should occur for exactly once in alternatives"); + static_assert(!is_void_v<_Tp>, "_Tp should not be void"); + return get<__detail::__variant::__index_of_v<_Tp, _Types...>>( + std::move(__v)); + } + + template + inline const _Tp& get(const variant<_Types...>& __v) + { + static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, + "T should occur for exactly once in alternatives"); + static_assert(!is_void_v<_Tp>, "_Tp should not be void"); + return get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v); + } + + template + inline const _Tp&& get(const variant<_Types...>&& __v) + { + static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, + "T should occur for exactly once in alternatives"); + static_assert(!is_void_v<_Tp>, "_Tp should not be void"); + return get<__detail::__variant::__index_of_v<_Tp, _Types...>>( + std::move(__v)); + } + + template + inline add_pointer_t>> + get_if(variant<_Types...>* __ptr) noexcept + { + using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>; + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void"); + if (__ptr && __ptr->index() == _Np) + return &__detail::__variant::__access<_Alternative_type>(*__ptr); + return nullptr; + } + + template + inline add_pointer_t>> + get_if(const variant<_Types...>* __ptr) noexcept + { + using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>; + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void"); + if (__ptr && __ptr->index() == _Np) + return &__detail::__variant::__access<_Alternative_type>(*__ptr); + return nullptr; + } + + template + inline add_pointer_t<_Tp> get_if(variant<_Types...>* __ptr) noexcept + { + static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, + "T should occur for exactly once in alternatives"); + static_assert(!is_void_v<_Tp>, "_Tp should not be void"); + return get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(__ptr); + } + + template + inline add_pointer_t get_if(const variant<_Types...>* __ptr) + noexcept + { + static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, + "T should occur for exactly once in alternatives"); + static_assert(!is_void_v<_Tp>, "_Tp should not be void"); + return get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(__ptr); + } + + template + bool operator==(const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) + { + if (__lhs.index() != __rhs.index()) + return false; + + if (__lhs.valueless_by_exception()) + return true; + + using __detail::__variant::__storage; + static constexpr bool (*_S_vtable[])(void*, void*) = + { &__detail::__variant::__erased_equal_to< + const __storage<_Types>&, const __storage<_Types>&>... }; + return _S_vtable[__lhs.index()]( + __detail::__variant::__get_storage(__lhs), + __detail::__variant::__get_storage(__rhs)); + } + + template + inline bool + operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) + { return !(__lhs == __rhs); } + + template + inline bool + operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) + { + if (__lhs.index() < __rhs.index()) + return true; + + if (__lhs.index() > __rhs.index()) + return false; + + if (__lhs.valueless_by_exception()) + return false; + + using __detail::__variant::__storage; + static constexpr bool (*_S_vtable[])(void*, void*) = + { &__detail::__variant::__erased_less_than< + const __storage<_Types>&, + const __storage<_Types>&>... }; + return _S_vtable[__lhs.index()]( + __detail::__variant::__get_storage(__lhs), + __detail::__variant::__get_storage(__rhs)); + } + + template + inline bool + operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) + { return __rhs < __lhs; } + + template + inline bool + operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) + { return !(__lhs > __rhs); } + + template + inline bool + operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) + { return !(__lhs < __rhs); } + + template + decltype(auto) visit(_Visitor&&, _Variants&&...); + + struct monostate { }; + + constexpr bool operator<(monostate, monostate) noexcept + { return false; } + + constexpr bool operator>(monostate, monostate) noexcept + { return false; } + + constexpr bool operator<=(monostate, monostate) noexcept + { return true; } + + constexpr bool operator>=(monostate, monostate) noexcept + { return true; } + + constexpr bool operator==(monostate, monostate) noexcept + { return true; } + + constexpr bool operator!=(monostate, monostate) noexcept + { return false; } + + template + inline auto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) + noexcept(noexcept(__lhs.swap(__rhs))) -> decltype(__lhs.swap(__rhs)) + { __lhs.swap(__rhs); } + + class bad_variant_access : public exception + { + public: + bad_variant_access() noexcept : _M_reason("Unknown reason") { } + const char* what() const noexcept override + { return _M_reason; } + + private: + bad_variant_access(const char* __reason) : _M_reason(__reason) { } + + const char* _M_reason; + + friend void __throw_bad_variant_access(const char* __what); + }; + + inline void + __throw_bad_variant_access(const char* __what) + { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); } + + template + class variant + : private __detail::__variant::_Variant_base<_Types...>, + private _Enable_default_constructor< + is_default_constructible_v< + variant_alternative_t<0, variant<_Types...>>>, variant<_Types...>>, + private _Enable_copy_move< + __and_...>::value, + __and_..., + is_move_constructible<_Types>..., + is_copy_assignable<_Types>...>::value, + __and_...>::value, + __and_..., + is_move_assignable<_Types>...>::value, + variant<_Types...>> + { + private: + using _Base = __detail::__variant::_Variant_base<_Types...>; + using _Default_ctor_enabler = + _Enable_default_constructor< + is_default_constructible_v< + variant_alternative_t<0, variant<_Types...>>>, variant<_Types...>>; + + template + static constexpr bool + __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>; + + template + static constexpr size_t __accepted_index = + __detail::__variant::__accepted_index<_Tp&&, variant>::value; + + template + struct __to_type_impl; + + template + struct __to_type_impl<_Np, true> + { using type = variant_alternative_t<_Np, variant>; }; + + template + using __to_type = typename __to_type_impl<_Np>::type; + + template + using __accepted_type = __to_type<__accepted_index<_Tp>>; + + template + using __storage = __detail::__variant::__storage<_Tp>; + + template + static constexpr size_t __index_of = + __detail::__variant::__index_of_v<_Tp, _Types...>; + + public: + constexpr variant() + noexcept(is_nothrow_default_constructible_v<__to_type<0>>) = default; + variant(const variant&) = default; + variant(variant&&) + noexcept(__and_< + is_nothrow_move_constructible<_Types>...>::value) = default; + + template> + && is_constructible_v<__accepted_type<_Tp&&>, _Tp&&> + && !is_same_v, variant>>> + constexpr + variant(_Tp&& __t) + noexcept(is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>) + : variant(in_place<__accepted_index<_Tp&&>>, forward<_Tp>(__t)) + { __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this)); } + + template + && is_constructible_v<_Tp, _Args&&...>>> + constexpr explicit + variant(in_place_type_t<_Tp>, _Args&&... __args) + : variant(in_place<__index_of<_Tp>>, forward<_Args>(__args)...) + { __glibcxx_assert(holds_alternative<_Tp>(*this)); } + + template + && is_constructible_v< + _Tp, initializer_list<_Up>&, _Args&&...>>> + constexpr explicit + variant(in_place_type_t<_Tp>, initializer_list<_Up> __il, + _Args&&... __args) + : variant(in_place<__index_of<_Tp>>, __il, + forward<_Args>(__args)...) + { __glibcxx_assert(holds_alternative<_Tp>(*this)); } + + template, _Args&&...>>> + constexpr explicit + variant(in_place_index_t<_Np>, _Args&&... __args) + : _Base(in_place<_Np>, forward<_Args>(__args)...), + _Default_ctor_enabler(_Enable_default_constructor_tag{}) + { __glibcxx_assert(index() == _Np); } + + template, + initializer_list<_Up>&, _Args&&...>>> + constexpr explicit + variant(in_place_index_t<_Np>, initializer_list<_Up> __il, + _Args&&... __args) + : _Base(in_place<_Np>, __il, forward<_Args>(__args)...), + _Default_ctor_enabler(_Enable_default_constructor_tag{}) + { __glibcxx_assert(index() == _Np); } + + template, _Alloc>>> + variant(allocator_arg_t, const _Alloc& __a) + : variant(allocator_arg, __a, in_place<0>) + { } + + template>>...>::value>> + variant(allocator_arg_t, const _Alloc& __a, const variant& __rhs) + : _Base(__a, __rhs), + _Default_ctor_enabler(_Enable_default_constructor_tag{}) + { } + + template>...>::value>> + variant(allocator_arg_t, const _Alloc& __a, variant&& __rhs) + : _Base(__a, std::move(__rhs)), + _Default_ctor_enabler(_Enable_default_constructor_tag{}) + { } + + template> + && __is_uses_allocator_constructible_v< + __accepted_type<_Tp&&>, _Alloc, _Tp&&> + && !is_same_v, variant>, variant&>> + variant(allocator_arg_t, const _Alloc& __a, _Tp&& __t) + : variant(allocator_arg, __a, in_place<__accepted_index<_Tp&&>>, + forward<_Tp>(__t)) + { __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this)); } + + template + && __is_uses_allocator_constructible_v< + _Tp, _Alloc, _Args&&...>>> + variant(allocator_arg_t, const _Alloc& __a, in_place_type_t<_Tp>, + _Args&&... __args) + : variant(allocator_arg, __a, in_place<__index_of<_Tp>>, + forward<_Args>(__args)...) + { __glibcxx_assert(holds_alternative<_Tp>(*this)); } + + template + && __is_uses_allocator_constructible_v< + _Tp, _Alloc, initializer_list<_Up>&, _Args&&...>>> + variant(allocator_arg_t, const _Alloc& __a, in_place_type_t<_Tp>, + initializer_list<_Up> __il, _Args&&... __args) + : variant(allocator_arg, __a, in_place<__index_of<_Tp>>, __il, + forward<_Args>(__args)...) + { __glibcxx_assert(holds_alternative<_Tp>(*this)); } + + template, _Alloc, _Args&&...>>> + variant(allocator_arg_t, const _Alloc& __a, in_place_index_t<_Np>, + _Args&&... __args) + : _Base(__a, in_place<_Np>, forward<_Args>(__args)...), + _Default_ctor_enabler(_Enable_default_constructor_tag{}) + { __glibcxx_assert(index() == _Np); } + + template, _Alloc, initializer_list<_Up>&, _Args&&...>>> + variant(allocator_arg_t, const _Alloc& __a, in_place_index_t<_Np>, + initializer_list<_Up> __il, _Args&&... __args) + : _Base(__a, in_place<_Np>, __il, forward<_Args>(__args)...), + _Default_ctor_enabler(_Enable_default_constructor_tag{}) + { __glibcxx_assert(index() == _Np); } + + ~variant() = default; + + variant& operator=(const variant&) = default; + variant& operator=(variant&&) + noexcept(__and_..., + is_nothrow_move_assignable<_Types>...>::value) = default; + + template + enable_if_t<__exactly_once<__accepted_type<_Tp&&>> + && is_constructible_v<__accepted_type<_Tp&&>, _Tp&&> + && is_assignable_v<__accepted_type<_Tp&&>&, _Tp&&> + && !is_same_v, variant>, variant&> + operator=(_Tp&& __rhs) + noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp&&> + && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>) + { + constexpr auto __index = __accepted_index<_Tp&&>; + if (index() == __index) + *static_cast<__storage<__to_type<__index>>*>(this->_M_storage()) + = forward<_Tp>(__rhs); + else + this->emplace<__index>(forward<_Tp>(__rhs)); + __glibcxx_assert(holds_alternative<__accepted_type<_Tp&&>>(*this)); + return *this; + } + + template + void emplace(_Args&&... __args) + { + static_assert(__exactly_once<_Tp>, + "T should occur for exactly once in alternatives"); + this->emplace<__index_of<_Tp>>(forward<_Args>(__args)...); + __glibcxx_assert(holds_alternative<_Tp>(*this)); + } + + template + void emplace(initializer_list<_Up> __il, _Args&&... __args) + { + static_assert(__exactly_once<_Tp>, + "T should occur for exactly once in alternatives"); + this->emplace<__index_of<_Tp>>(__il, forward<_Args>(__args)...); + __glibcxx_assert(holds_alternative<_Tp>(*this)); + } + + template + void emplace(_Args&&... __args) + { + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + this->~variant(); + __try + { + ::new (this) variant(in_place<_Np>, + forward<_Args>(__args)...); + } + __catch (...) + { + this->_M_index = variant_npos; + __throw_exception_again; + } + __glibcxx_assert(index() == _Np); + } + + template + void emplace(initializer_list<_Up> __il, _Args&&... __args) + { + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + this->~variant(); + __try + { + ::new (this) variant(in_place<_Np>, __il, + forward<_Args>(__args)...); + } + __catch (...) + { + this->_M_index = variant_npos; + __throw_exception_again; + } + __glibcxx_assert(index() == _Np); + } + + constexpr bool valueless_by_exception() const noexcept + { return !this->_M_valid(); } + + constexpr size_t index() const noexcept + { return this->_M_index; } + + void + swap(variant& __rhs) + noexcept(__and_<__is_nothrow_swappable<_Types>...>::value + && is_nothrow_move_assignable_v) + { + if (this->index() == __rhs.index()) + { + if (this->_M_valid()) + { + static constexpr void (*_S_vtable[])(void*, void*) = + { &__detail::__variant::__erased_swap< + __storage<_Types>&, __storage<_Types>&>... }; + _S_vtable[__rhs._M_index](this->_M_storage(), + __rhs._M_storage()); + } + } + else if (!this->_M_valid()) + { + *this = std::move(__rhs); + } + else if (!__rhs._M_valid()) + { + __rhs = std::move(*this); + } + else + { + auto __tmp = std::move(__rhs); + __rhs = std::move(*this); + *this = std::move(__tmp); + } + } + + template + friend void* __detail::__variant::__get_storage(_Vp&& __v); + }; + + // To honor algebraic data type, variant<> should be a bottom type, which + // is 0 (as opposed to a void type, which is 1). Use incomplete type to model + // bottom type. + template<> class variant<>; + + template + variant_alternative_t<_Np, variant<_Types...>>& + get(variant<_Types...>& __v) + { + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + if (__v.index() != _Np) + __throw_bad_variant_access("Unexpected index"); + return __detail::__variant::__access< + variant_alternative_t<_Np, variant<_Types...>>>(__v); + } + + template + variant_alternative_t<_Np, variant<_Types...>>&& + get(variant<_Types...>&& __v) + { + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + if (__v.index() != _Np) + __throw_bad_variant_access("Unexpected index"); + return __detail::__variant::__access< + variant_alternative_t<_Np, variant<_Types...>>>(std::move(__v)); + } + + template + const variant_alternative_t<_Np, variant<_Types...>>& + get(const variant<_Types...>& __v) + { + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + if (__v.index() != _Np) + __throw_bad_variant_access("Unexpected index"); + return __detail::__variant::__access< + variant_alternative_t<_Np, variant<_Types...>>>(__v); + } + + template + const variant_alternative_t<_Np, variant<_Types...>>&& + get(const variant<_Types...>&& __v) + { + static_assert(_Np < sizeof...(_Types), + "The index should be in [0, number of alternatives)"); + if (__v.index() != _Np) + __throw_bad_variant_access("Unexpected index"); + return __detail::__variant::__access< + variant_alternative_t<_Np, variant<_Types...>>>(std::move(__v)); + } + + template + decltype(auto) + visit(_Visitor&& __visitor, _Variants&&... __variants) + { + using _Result_type = + decltype(forward<_Visitor>(__visitor)(get<0>(__variants)...)); + static constexpr auto _S_vtable = + __detail::__variant::__gen_vtable< + _Result_type, _Visitor&&, _Variants&&...>::_S_apply(); + auto __func_ptr = _S_vtable._M_access(__variants.index()...); + return (*__func_ptr)(forward<_Visitor>(__visitor), + __detail::__variant::__get_storage(__variants)...); + } + + template + struct uses_allocator, _Alloc> + : true_type { }; + + template + struct hash> + { + using result_type = size_t; + using argument_type = variant<_Types...>; + + size_t + operator()(const variant<_Types...>& __t) const + noexcept((... && noexcept(hash>{}(std::declval<_Types>())))) + { + if (!__t.valueless_by_exception()) + { + namespace __edv = __detail::__variant; + static constexpr size_t (*_S_vtable[])(void*) = + { &__edv::__erased_hash&>... }; + return hash{}(__t.index()) + + _S_vtable[__t.index()](__edv::__get_storage(__t)); + } + return hash{}(__t.index()); + } + }; + + template<> + struct hash + { + using result_type = size_t; + using argument_type = monostate; + + size_t + operator()(const monostate& __t) const noexcept + { + constexpr size_t __magic_monostate_hash = -7777; + return __magic_monostate_hash; + } + }; + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace std + +#endif // C++17 + +#endif // _GLIBCXX_VARIANT diff --git a/libstdc++-v3/testsuite/20_util/variant/compile.cc b/libstdc++-v3/testsuite/20_util/variant/compile.cc new file mode 100644 index 00000000000..b57d356d301 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/compile.cc @@ -0,0 +1,405 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 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 copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include + +using namespace std; + +struct AllDeleted +{ + AllDeleted() = delete; + AllDeleted(const AllDeleted&) = delete; + AllDeleted(AllDeleted&&) = delete; + AllDeleted& operator=(const AllDeleted&) = delete; + AllDeleted& operator=(AllDeleted&&) = delete; +}; + +struct Empty +{ + Empty() { }; + Empty(const Empty&) { }; + Empty(Empty&&) { }; + Empty& operator=(const Empty&) { return *this; }; + Empty& operator=(Empty&&) { return *this; }; +}; + +struct DefaultNoexcept +{ + DefaultNoexcept() noexcept = default; + DefaultNoexcept(const DefaultNoexcept&) noexcept = default; + DefaultNoexcept(DefaultNoexcept&&) noexcept = default; + DefaultNoexcept& operator=(const DefaultNoexcept&) noexcept = default; + DefaultNoexcept& operator=(DefaultNoexcept&&) noexcept = default; +}; + +void default_ctor() +{ + static_assert(is_default_constructible_v>, ""); + static_assert(is_default_constructible_v>, ""); + static_assert(!is_default_constructible_v>, ""); + static_assert(!is_default_constructible_v>, ""); + static_assert(is_default_constructible_v>, ""); + + static_assert(noexcept(variant()), ""); + static_assert(!noexcept(variant()), ""); + static_assert(noexcept(variant()), ""); +} + +void copy_ctor() +{ + static_assert(is_copy_constructible_v>, ""); + static_assert(!is_copy_constructible_v>, ""); + + { + variant a; + static_assert(!noexcept(variant(a)), ""); + } + { + variant a; + static_assert(!noexcept(variant(a)), ""); + } + { + variant a; + static_assert(!noexcept(variant(a)), ""); + } + { + variant a; + static_assert(!noexcept(variant(a)), ""); + } +} + +void move_ctor() +{ + static_assert(is_move_constructible_v>, ""); + static_assert(!is_move_constructible_v>, ""); + static_assert(!noexcept(variant(variant())), ""); + static_assert(noexcept(variant(variant())), ""); +} + +void arbitrary_ctor() +{ + static_assert(!is_constructible_v, const char*>, ""); + static_assert(is_constructible_v, const char*>, ""); + static_assert(noexcept(variant(int{})), ""); + static_assert(noexcept(variant(int{})), ""); + static_assert(!noexcept(variant(Empty{})), ""); + static_assert(noexcept(variant(DefaultNoexcept{})), ""); +} + +void in_place_index_ctor() +{ + variant a(in_place<0>, "a"); + variant b(in_place<1>, {'a'}); +} + +void in_place_type_ctor() +{ + variant a(in_place, "a"); + variant b(in_place, {'a'}); + static_assert(!is_constructible_v, in_place_type_t, const char*>, ""); +} + +void uses_alloc_ctors() +{ + std::allocator alloc; + variant a(allocator_arg, alloc); + static_assert(!is_constructible_v, allocator_arg_t, std::allocator>, ""); + { + variant b(allocator_arg, alloc, a); + static_assert(!is_constructible_v, allocator_arg_t, std::allocator, const variant&>, ""); + } + { + variant b(allocator_arg, alloc, std::move(a)); + static_assert(!is_constructible_v, allocator_arg_t, std::allocator, variant&&>, ""); + } + { + variant b(allocator_arg, alloc, "a"); + static_assert(!is_constructible_v, allocator_arg_t, std::allocator, const char*>, ""); + } + { + variant b(allocator_arg, alloc, in_place<0>, "a"); + variant c(allocator_arg, alloc, in_place<1>, "a"); + } + { + variant b(allocator_arg, alloc, in_place<0>, {'a'}); + variant c(allocator_arg, alloc, in_place<1>, {'a'}); + } + { + variant b(allocator_arg, alloc, in_place, "a"); + } + { + variant b(allocator_arg, alloc, in_place, {'a'}); + } +} + +void dtor() +{ + static_assert(is_destructible_v>, ""); + static_assert(is_destructible_v>, ""); +} + +void copy_assign() +{ + static_assert(is_copy_assignable_v>, ""); + static_assert(!is_copy_assignable_v>, ""); + { + variant a; + static_assert(!noexcept(a = a), ""); + } + { + variant a; + static_assert(!noexcept(a = a), ""); + } +} + +void move_assign() +{ + static_assert(is_move_assignable_v>, ""); + static_assert(!is_move_assignable_v>, ""); + { + variant a; + static_assert(!noexcept(a = std::move(a)), ""); + } + { + variant a; + static_assert(noexcept(a = std::move(a)), ""); + } +} + +void arbitrary_assign() +{ + static_assert(!is_assignable_v, const char*>, ""); + static_assert(is_assignable_v, const char*>, ""); + static_assert(noexcept(variant() = int{}), ""); + static_assert(noexcept(variant() = int{}), ""); + static_assert(!noexcept(variant() = Empty{}), ""); + static_assert(noexcept(variant() = DefaultNoexcept{}), ""); +} + +void test_get() +{ + { + static_assert(is_same(variant())), int&&>::value, ""); + static_assert(is_same(variant())), string&&>::value, ""); + static_assert(is_same(variant())), string&>::value, ""); + static_assert(is_same(variant())), string&&>::value, ""); + static_assert(is_same(variant())), const string&&>::value, ""); + static_assert(is_same(variant())), const string&>::value, ""); + static_assert(is_same(variant())), const string&&>::value, ""); + + static_assert(is_same(variant())), int&&>::value, ""); + static_assert(is_same(variant())), string&&>::value, ""); + static_assert(is_same(variant())), string&>::value, ""); + static_assert(is_same(variant())), string&&>::value, ""); + static_assert(is_same(variant())), const string&&>::value, ""); + static_assert(is_same(variant())), const string&>::value, ""); + static_assert(is_same(variant())), const string&&>::value, ""); + } + { + variant a; + variant b; + variant c; + variant d; + variant e; + variant f; + + static_assert(is_same(a)), int&>::value, ""); + static_assert(is_same(a)), string&>::value, ""); + static_assert(is_same(b)), string&>::value, ""); + static_assert(is_same(c)), string&>::value, ""); + static_assert(is_same(e)), const string&>::value, ""); + static_assert(is_same(e)), const string&>::value, ""); + static_assert(is_same(f)), const string&>::value, ""); + + static_assert(is_same(a)), int&>::value, ""); + static_assert(is_same(a)), string&>::value, ""); + static_assert(is_same(b)), string&>::value, ""); + static_assert(is_same(c)), string&>::value, ""); + static_assert(is_same(e)), const string&>::value, ""); + static_assert(is_same(e)), const string&>::value, ""); + static_assert(is_same(f)), const string&>::value, ""); + + static_assert(is_same(&a)), int*>::value, ""); + static_assert(is_same(&a)), string*>::value, ""); + static_assert(is_same(&b)), string*>::value, ""); + static_assert(is_same(&c)), string*>::value, ""); + static_assert(is_same(&e)), const string*>::value, ""); + static_assert(is_same(&e)), const string*>::value, ""); + static_assert(is_same(&f)), const string*>::value, ""); + + static_assert(is_same(&a)), int*>::value, ""); + static_assert(is_same(&a)), string*>::value, ""); + static_assert(is_same(&b)), string*>::value, ""); + static_assert(is_same(&c)), string*>::value, ""); + static_assert(is_same(&e)), const string*>::value, ""); + static_assert(is_same(&e)), const string*>::value, ""); + static_assert(is_same(&f)), const string*>::value, ""); + } + { + const variant a; + const variant b; + const variant c; + const variant d; + const variant e; + const variant f; + + static_assert(is_same(a)), const int&>::value, ""); + static_assert(is_same(a)), const string&>::value, ""); + static_assert(is_same(b)), string&>::value, ""); + static_assert(is_same(c)), string&>::value, ""); + static_assert(is_same(d)), const string&>::value, ""); + static_assert(is_same(e)), const string&>::value, ""); + static_assert(is_same(f)), const string&>::value, ""); + + static_assert(is_same(a)), const int&>::value, ""); + static_assert(is_same(a)), const string&>::value, ""); + static_assert(is_same(b)), string&>::value, ""); + static_assert(is_same(c)), string&>::value, ""); + static_assert(is_same(d)), const string&>::value, ""); + static_assert(is_same(e)), const string&>::value, ""); + static_assert(is_same(f)), const string&>::value, ""); + + static_assert(is_same(&a)), const int*>::value, ""); + static_assert(is_same(&a)), const string*>::value, ""); + static_assert(is_same(&b)), string*>::value, ""); + static_assert(is_same(&c)), string*>::value, ""); + static_assert(is_same(&d)), const string*>::value, ""); + static_assert(is_same(&e)), const string*>::value, ""); + static_assert(is_same(&f)), const string*>::value, ""); + + static_assert(is_same(&a)), const int*>::value, ""); + static_assert(is_same(&a)), const string*>::value, ""); + static_assert(is_same(&b)), string*>::value, ""); + static_assert(is_same(&c)), string*>::value, ""); + static_assert(is_same(&d)), const string*>::value, ""); + static_assert(is_same(&e)), const string*>::value, ""); + static_assert(is_same(&f)), const string*>::value, ""); + } +} + +void test_relational() +{ + { + const variant a, b; + (void)(a < b); + (void)(a > b); + (void)(a <= b); + (void)(a == b); + (void)(a != b); + (void)(a >= b); + } + { + const monostate a, b; + (void)(a < b); + (void)(a > b); + (void)(a <= b); + (void)(a == b); + (void)(a != b); + (void)(a >= b); + } +} + +void test_swap() +{ + variant a, b; + a.swap(b); + swap(a, b); +} + +void test_visit() +{ + { + struct Visitor + { + void operator()(monostate) {} + void operator()(const int&) {} + }; + struct CVisitor + { + void operator()(monostate) const {} + void operator()(const int&) const {} + }; + variant a; + const variant b; + Visitor v; + const CVisitor u; + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + } + { + struct Visitor + { + bool operator()(int, float) { return false; } + bool operator()(int, double) { return false; } + bool operator()(char, float) { return false; } + bool operator()(char, double) { return false; } + }; + visit(Visitor(), variant(), variant()); + } +} + +void test_constexpr() +{ + constexpr variant a; + static_assert(holds_alternative(a), ""); + constexpr variant b(in_place<0>, int{}); + static_assert(holds_alternative(b), ""); + constexpr variant c(in_place, int{}); + static_assert(holds_alternative(c), ""); + constexpr variant d(in_place<1>, char{}); + static_assert(holds_alternative(d), ""); + constexpr variant e(in_place, char{}); + static_assert(holds_alternative(e), ""); + constexpr variant f(char{}); + static_assert(holds_alternative(f), ""); + + { + struct literal { + constexpr literal() = default; + }; + + struct nonliteral { + nonliteral() { } + }; + + constexpr variant v{}; + constexpr variant v1{in_place}; + constexpr variant v2{in_place<0>}; + } +} + +void test_void() +{ + static_assert(is_same(variant()))>::value, ""); + static_assert(!is_default_constructible_v>, ""); + static_assert(!is_copy_constructible_v>, ""); + static_assert(!is_move_constructible_v>, ""); + static_assert(!is_copy_assignable_v>, ""); + static_assert(!is_move_assignable_v>, ""); + variant v; + v = 3; + v = "asdf"; +} diff --git a/libstdc++-v3/testsuite/20_util/variant/run.cc b/libstdc++-v3/testsuite/20_util/variant/run.cc new file mode 100644 index 00000000000..cbe3b17fd7a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/run.cc @@ -0,0 +1,501 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 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 copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include +#include +#include + +using namespace std; + +struct AlwaysThrow +{ + AlwaysThrow() = default; + + AlwaysThrow(const AlwaysThrow&) + { throw nullptr; } + + AlwaysThrow(AlwaysThrow&&) + { throw nullptr; } + + AlwaysThrow& operator=(const AlwaysThrow&) + { + throw nullptr; + return *this; + } + + AlwaysThrow& operator=(AlwaysThrow&&) + { + throw nullptr; + return *this; + } +}; + +void default_ctor() +{ + bool test [[gnu::unused]] = true; + + variant v; + VERIFY(holds_alternative(v)); +} + +void copy_ctor() +{ + bool test [[gnu::unused]] = true; + + variant v("a"); + VERIFY(holds_alternative(v)); + variant u(v); + VERIFY(holds_alternative(u)); + VERIFY(get(u) == "a"); +} + +void move_ctor() +{ + bool test [[gnu::unused]] = true; + + variant v("a"); + VERIFY(holds_alternative(v)); + variant u(std::move(v)); + VERIFY(holds_alternative(u)); + VERIFY(get(u) == "a"); + VERIFY(holds_alternative(v)); +} + +void arbitrary_ctor() +{ + bool test [[gnu::unused]] = true; + + variant v("a"); + VERIFY(holds_alternative(v)); + VERIFY(get<1>(v) == "a"); +} + +void copy_assign() +{ + bool test [[gnu::unused]] = true; + + variant v("a"); + VERIFY(holds_alternative(v)); + variant u; + u = v; + VERIFY(holds_alternative(u)); + VERIFY(get(u) == "a"); +} + +void move_assign() +{ + bool test [[gnu::unused]] = true; + + variant v("a"); + VERIFY(holds_alternative(v)); + variant u; + u = std::move(v); + VERIFY(holds_alternative(u)); + VERIFY(get(u) == "a"); + VERIFY(holds_alternative(v)); +} + +void arbitrary_assign() +{ + bool test [[gnu::unused]] = true; + + variant v; + v = "a"; + + VERIFY(holds_alternative(variant("a"))); + VERIFY(get<1>(v) == "a"); +} + +void dtor() +{ + bool test [[gnu::unused]] = true; + + struct A { + A(int& called) : called(called) {} + ~A() { + called++; + } + int& called; + }; + { + int called = 0; + { variant a(in_place<1>, called); } + VERIFY(called == 1); + } + { + int called = 0; + { variant a(in_place<0>); } + VERIFY(called == 0); + } +} + +void in_place_index_ctor() +{ + bool test [[gnu::unused]] = true; + + { + variant v(in_place<1>, "a"); + VERIFY(holds_alternative(v)); + VERIFY(get<1>(v) == "a"); + } + { + variant v(in_place<1>, {'a', 'b'}); + VERIFY(holds_alternative(v)); + VERIFY(get<1>(v) == "ab"); + } +} + +void in_place_type_ctor() +{ + bool test [[gnu::unused]] = true; + + { + variant v(in_place, "a"); + VERIFY(holds_alternative(v)); + VERIFY(get<1>(v) == "a"); + } + { + variant v(in_place, {'a', 'b'}); + VERIFY(holds_alternative(v)); + VERIFY(get<1>(v) == "ab"); + } +} + +struct UsesAllocatable +{ + template + UsesAllocatable(std::allocator_arg_t, const Alloc& a) + : d(0), a(static_cast(&a)) { } + + template + UsesAllocatable(std::allocator_arg_t, const Alloc& a, const UsesAllocatable&) + : d(1), a(static_cast(&a)) { } + + template + UsesAllocatable(std::allocator_arg_t, const Alloc& a, UsesAllocatable&&) + : d(2), a(static_cast(&a)) { } + + int d; + const void* a; +}; + +namespace std +{ + template<> + struct uses_allocator> : true_type { }; +} + +void uses_allocator_ctor() +{ + bool test [[gnu::unused]] = true; + + std::allocator a; + variant v(std::allocator_arg, a); + VERIFY(get<0>(v).d == 0); + VERIFY(get<0>(v).a == &a); + { + variant u(std::allocator_arg, a, v); + VERIFY(get<0>(u).d == 1); + VERIFY(get<0>(u).a == &a); + } + { + variant u(std::allocator_arg, a, std::move(v)); + VERIFY(get<0>(u).d == 2); + VERIFY(get<0>(u).a == &a); + } +} + +void emplace() +{ + bool test [[gnu::unused]] = true; + + variant v; + v.emplace<0>(1); + VERIFY(get<0>(v) == 1); + v.emplace("a"); + VERIFY(get(v) == "a"); + v.emplace<1>({'a', 'b'}); + VERIFY(get<1>(v) == "ab"); + v.emplace({'a', 'c'}); + VERIFY(get(v) == "ac"); + { + variant v; + AlwaysThrow a; + try { v.emplace<1>(a); } catch (nullptr_t) { } + VERIFY(v.valueless_by_exception()); + } + { + variant v; + try { v.emplace<1>(AlwaysThrow{}); } catch (nullptr_t) { } + VERIFY(v.valueless_by_exception()); + } +} + +void test_get() +{ + bool test [[gnu::unused]] = true; + + VERIFY(get<1>(variant("a")) == "a"); + VERIFY(get(variant("a")) == "a"); + { + bool caught = false; + + try + { + get<0>(variant("a")); + } + catch (const bad_variant_access&) + { + caught = true; + } + VERIFY(caught); + } + { + bool caught = false; + + try + { + get(variant("a")); + } + catch (const bad_variant_access&) + { + caught = true; + } + VERIFY(caught); + } +} + +void test_relational() +{ + bool test [[gnu::unused]] = true; + + VERIFY((variant(2) < variant(3))); + VERIFY((variant(3) == variant(3))); + VERIFY((variant(3) > variant(2))); + VERIFY((variant(3) <= variant(3))); + VERIFY((variant(2) <= variant(3))); + VERIFY((variant(3) >= variant(3))); + VERIFY((variant(3) >= variant(2))); + VERIFY((variant(2) != variant(3))); + + VERIFY((variant(2) < variant("a"))); + VERIFY((variant(2) > variant("a"))); +} + +void test_swap() +{ + bool test [[gnu::unused]] = true; + + variant a("a"), b("b"); + a.swap(b); + VERIFY(get<1>(a) == "b"); + VERIFY(get<1>(b) == "a"); + swap(a, b); + VERIFY(get<1>(a) == "a"); + VERIFY(get<1>(b) == "b"); +} + +void test_visit() +{ + bool test [[gnu::unused]] = true; + + { + struct Visitor + { + int operator()(int, float) { + return 0; + } + int operator()(int, double) { + return 1; + } + int operator()(char, float) { + return 2; + } + int operator()(char, double) { + return 3; + } + int operator()(int, float) const { + return 5; + } + int operator()(int, double) const { + return 6; + } + int operator()(char, float) const { + return 7; + } + int operator()(char, double) const { + return 8; + } + } visitor1; + VERIFY(visit(visitor1, variant(1), variant(1.0f)) == 0); + VERIFY(visit(visitor1, variant(1), variant(1.0)) == 1); + VERIFY(visit(visitor1, variant('a'), variant(1.0f)) == 2); + VERIFY(visit(visitor1, variant('a'), variant(1.0)) == 3); + + const auto& visitor2 = visitor1; + VERIFY(visit(visitor2, variant(1), variant(1.0f)) == 5); + VERIFY(visit(visitor2, variant(1), variant(1.0)) == 6); + VERIFY(visit(visitor2, variant('a'), variant(1.0f)) == 7); + VERIFY(visit(visitor2, variant('a'), variant(1.0)) == 8); + } + + { + struct Visitor + { + int operator()(int, float) && { + return 0; + } + int operator()(int, double) && { + return 1; + } + int operator()(char, float) && { + return 2; + } + int operator()(char, double) && { + return 3; + } + }; + VERIFY(visit(Visitor{}, variant(1), variant(1.0f)) == 0); + VERIFY(visit(Visitor{}, variant(1), variant(1.0)) == 1); + VERIFY(visit(Visitor{}, variant('a'), variant(1.0f)) == 2); + VERIFY(visit(Visitor{}, variant('a'), variant(1.0)) == 3); + } +} + +void test_hash() +{ + bool test [[gnu::unused]] = true; + + unordered_set> s; + VERIFY(s.emplace(3).second); + VERIFY(s.emplace("asdf").second); + VERIFY(s.emplace().second); + VERIFY(s.size() == 3); + VERIFY(!s.emplace(3).second); + VERIFY(!s.emplace("asdf").second); + VERIFY(!s.emplace().second); + VERIFY(s.size() == 3); + { + struct A + { + operator int() + { + throw nullptr; + } + }; + variant v; + try + { + v.emplace<0>(A{}); + } + catch (nullptr_t) + { + } + VERIFY(v.valueless_by_exception()); + VERIFY(s.insert(v).second); + VERIFY(s.size() == 4); + VERIFY(!s.insert(v).second); + } +} + +void test_valueless_by_exception() +{ + bool test [[gnu::unused]] = true; + + { + AlwaysThrow a; + bool caught = false; + try + { + variant v(a); + } + catch (nullptr_t) + { + caught = true; + } + VERIFY(caught); + } + { + AlwaysThrow a; + bool caught = false; + try + { + variant v(a); + } + catch (nullptr_t) + { + caught = true; + } + VERIFY(caught); + } + { + variant v; + bool caught = false; + try + { + AlwaysThrow a; + v = a; + } + catch (nullptr_t) + { + caught = true; + } + VERIFY(caught); + VERIFY(v.valueless_by_exception()); + } + { + variant v; + bool caught = false; + try + { + v = AlwaysThrow{}; + } + catch (nullptr_t) + { + caught = true; + } + VERIFY(caught); + VERIFY(v.valueless_by_exception()); + } +} + +int main() +{ + default_ctor(); + copy_ctor(); + move_ctor(); + arbitrary_ctor(); + in_place_index_ctor(); + in_place_type_ctor(); + uses_allocator_ctor(); + copy_assign(); + move_assign(); + arbitrary_assign(); + dtor(); + emplace(); + test_get(); + test_relational(); + test_swap(); + test_visit(); + test_hash(); + test_valueless_by_exception(); +} -- 2.30.2