15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
95a83710eSEric Fiselier // <tuple>
105a83710eSEric Fiselier
115a83710eSEric Fiselier // template <class... Types> class tuple;
125a83710eSEric Fiselier
135a83710eSEric Fiselier // template <class Alloc, class... UTypes>
145a83710eSEric Fiselier // tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&&);
155a83710eSEric Fiselier
1631cbe0f2SLouis Dionne // UNSUPPORTED: c++03
170a52cd79SEric Fiselier
185a83710eSEric Fiselier #include <tuple>
195a83710eSEric Fiselier #include <string>
205a83710eSEric Fiselier #include <memory>
215a83710eSEric Fiselier #include <cassert>
225a83710eSEric Fiselier
237fc6a556SMarshall Clow #include "test_macros.h"
245a83710eSEric Fiselier #include "allocators.h"
2516719cd0SHui Xie #include "test_allocator.h"
265a83710eSEric Fiselier #include "../alloc_first.h"
275a83710eSEric Fiselier #include "../alloc_last.h"
285a83710eSEric Fiselier
295a83710eSEric Fiselier struct B
305a83710eSEric Fiselier {
315a83710eSEric Fiselier int id_;
325a83710eSEric Fiselier
BB335a83710eSEric Fiselier explicit B(int i) : id_(i) {}
345a83710eSEric Fiselier
~BB355a83710eSEric Fiselier virtual ~B() {}
365a83710eSEric Fiselier };
375a83710eSEric Fiselier
385a83710eSEric Fiselier struct D
395a83710eSEric Fiselier : B
405a83710eSEric Fiselier {
DD415a83710eSEric Fiselier explicit D(int i) : B(i) {}
425a83710eSEric Fiselier };
435a83710eSEric Fiselier
449795699aSEric Fiselier struct Explicit {
459795699aSEric Fiselier int value;
ExplicitExplicit469795699aSEric Fiselier explicit Explicit(int x) : value(x) {}
479795699aSEric Fiselier };
489795699aSEric Fiselier
499795699aSEric Fiselier struct Implicit {
509795699aSEric Fiselier int value;
ImplicitImplicit519795699aSEric Fiselier Implicit(int x) : value(x) {}
529795699aSEric Fiselier };
539795699aSEric Fiselier
54*24186f72Scpplearner #if TEST_STD_VER > 17
alloc_move_constructor_is_constexpr()5516719cd0SHui Xie constexpr bool alloc_move_constructor_is_constexpr() {
5616719cd0SHui Xie std::tuple<int> t1 = 1;
5716719cd0SHui Xie std::tuple<int> t2 = {std::allocator_arg, test_allocator<int>{}, std::move(t1)};
5816719cd0SHui Xie assert(std::get<0>(t2) == 1);
5916719cd0SHui Xie return true;
6016719cd0SHui Xie }
6116719cd0SHui Xie #endif
6216719cd0SHui Xie
main(int,char **)632df59c50SJF Bastien int main(int, char**)
645a83710eSEric Fiselier {
655a83710eSEric Fiselier {
665a83710eSEric Fiselier typedef std::tuple<int> T0;
675a83710eSEric Fiselier typedef std::tuple<alloc_first> T1;
685a83710eSEric Fiselier T0 t0(2);
695a83710eSEric Fiselier alloc_first::allocator_constructed = false;
705a83710eSEric Fiselier T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
715a83710eSEric Fiselier assert(alloc_first::allocator_constructed);
725a83710eSEric Fiselier assert(std::get<0>(t1) == 2);
735a83710eSEric Fiselier }
745a83710eSEric Fiselier {
755a83710eSEric Fiselier typedef std::tuple<std::unique_ptr<D>> T0;
765a83710eSEric Fiselier typedef std::tuple<std::unique_ptr<B>> T1;
775a83710eSEric Fiselier T0 t0(std::unique_ptr<D>(new D(3)));
785a83710eSEric Fiselier T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
795a83710eSEric Fiselier assert(std::get<0>(t1)->id_ == 3);
805a83710eSEric Fiselier }
815a83710eSEric Fiselier {
825a83710eSEric Fiselier typedef std::tuple<int, std::unique_ptr<D>> T0;
835a83710eSEric Fiselier typedef std::tuple<alloc_first, std::unique_ptr<B>> T1;
845a83710eSEric Fiselier T0 t0(2, std::unique_ptr<D>(new D(3)));
855a83710eSEric Fiselier alloc_first::allocator_constructed = false;
865a83710eSEric Fiselier T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
875a83710eSEric Fiselier assert(alloc_first::allocator_constructed);
885a83710eSEric Fiselier assert(std::get<0>(t1) == 2);
895a83710eSEric Fiselier assert(std::get<1>(t1)->id_ == 3);
905a83710eSEric Fiselier }
915a83710eSEric Fiselier {
925a83710eSEric Fiselier typedef std::tuple<int, int, std::unique_ptr<D>> T0;
935a83710eSEric Fiselier typedef std::tuple<alloc_last, alloc_first, std::unique_ptr<B>> T1;
945a83710eSEric Fiselier T0 t0(1, 2, std::unique_ptr<D>(new D(3)));
955a83710eSEric Fiselier alloc_first::allocator_constructed = false;
965a83710eSEric Fiselier alloc_last::allocator_constructed = false;
975a83710eSEric Fiselier T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
985a83710eSEric Fiselier assert(alloc_first::allocator_constructed);
995a83710eSEric Fiselier assert(alloc_last::allocator_constructed);
1005a83710eSEric Fiselier assert(std::get<0>(t1) == 1);
1015a83710eSEric Fiselier assert(std::get<1>(t1) == 2);
1025a83710eSEric Fiselier assert(std::get<2>(t1)->id_ == 3);
1035a83710eSEric Fiselier }
1049795699aSEric Fiselier {
1059795699aSEric Fiselier std::tuple<int> t1(42);
1065b1e5b43SMichael Park std::tuple<Explicit> t2{std::allocator_arg, std::allocator<int>{}, std::move(t1)};
1079795699aSEric Fiselier assert(std::get<0>(t2).value == 42);
1089795699aSEric Fiselier }
1099795699aSEric Fiselier {
1109795699aSEric Fiselier std::tuple<int> t1(42);
1115b1e5b43SMichael Park std::tuple<Implicit> t2 = {std::allocator_arg, std::allocator<int>{}, std::move(t1)};
1129795699aSEric Fiselier assert(std::get<0>(t2).value == 42);
1139795699aSEric Fiselier }
114a3ab5120SLouis Dionne {
115a3ab5120SLouis Dionne // Test that we can use a tag derived from allocator_arg_t
116a3ab5120SLouis Dionne struct DerivedFromAllocatorArgT : std::allocator_arg_t { };
117a3ab5120SLouis Dionne DerivedFromAllocatorArgT derived;
118a3ab5120SLouis Dionne std::tuple<long> from(3l);
119a3ab5120SLouis Dionne std::tuple<long long> t0(derived, A1<int>(), std::move(from));
120a3ab5120SLouis Dionne }
1212df59c50SJF Bastien
122*24186f72Scpplearner #if TEST_STD_VER > 17
12316719cd0SHui Xie static_assert(alloc_move_constructor_is_constexpr());
12416719cd0SHui Xie #endif
12516719cd0SHui Xie
1262df59c50SJF Bastien return 0;
1275a83710eSEric Fiselier }
128