xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/alloc.pass.cpp (revision a3ab5120fd572215afeac190757834a041dda73a)
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 
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03
1009238677SEric Fiselier 
115a83710eSEric Fiselier // <tuple>
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // template <class... Types> class tuple;
145a83710eSEric Fiselier 
155a83710eSEric Fiselier // template <class Alloc>
1659e26308SLouis Dionne //   explicit(see-below) tuple(allocator_arg_t, const Alloc& a);
175a83710eSEric Fiselier 
185a83710eSEric Fiselier #include <tuple>
195a83710eSEric Fiselier #include <cassert>
205a83710eSEric Fiselier 
217fc6a556SMarshall Clow #include "test_macros.h"
225a83710eSEric Fiselier #include "DefaultOnly.h"
235a83710eSEric Fiselier #include "allocators.h"
245a83710eSEric Fiselier #include "../alloc_first.h"
255a83710eSEric Fiselier #include "../alloc_last.h"
265a83710eSEric Fiselier 
2790fb2bafSEric Fiselier template <class T = void>
2890fb2bafSEric Fiselier struct NonDefaultConstructible {
NonDefaultConstructibleNonDefaultConstructible2990fb2bafSEric Fiselier   constexpr NonDefaultConstructible() {
3090fb2bafSEric Fiselier       static_assert(!std::is_same<T, T>::value, "Default Ctor instantiated");
3190fb2bafSEric Fiselier   }
3290fb2bafSEric Fiselier 
NonDefaultConstructibleNonDefaultConstructible3390fb2bafSEric Fiselier   explicit constexpr NonDefaultConstructible(int) {}
3490fb2bafSEric Fiselier };
3590fb2bafSEric Fiselier 
3690fb2bafSEric Fiselier 
3790fb2bafSEric Fiselier struct DerivedFromAllocArgT : std::allocator_arg_t {};
3890fb2bafSEric Fiselier 
main(int,char **)392df59c50SJF Bastien int main(int, char**)
405a83710eSEric Fiselier {
415a83710eSEric Fiselier     {
425a83710eSEric Fiselier         std::tuple<> t(std::allocator_arg, A1<int>());
435a83710eSEric Fiselier     }
445a83710eSEric Fiselier     {
455a83710eSEric Fiselier         std::tuple<int> t(std::allocator_arg, A1<int>());
465a83710eSEric Fiselier         assert(std::get<0>(t) == 0);
475a83710eSEric Fiselier     }
485a83710eSEric Fiselier     {
495a83710eSEric Fiselier         std::tuple<DefaultOnly> t(std::allocator_arg, A1<int>());
505a83710eSEric Fiselier         assert(std::get<0>(t) == DefaultOnly());
515a83710eSEric Fiselier     }
525a83710eSEric Fiselier     {
535a83710eSEric Fiselier         assert(!alloc_first::allocator_constructed);
545a83710eSEric Fiselier         std::tuple<alloc_first> t(std::allocator_arg, A1<int>(5));
555a83710eSEric Fiselier         assert(alloc_first::allocator_constructed);
565a83710eSEric Fiselier         assert(std::get<0>(t) == alloc_first());
575a83710eSEric Fiselier     }
585a83710eSEric Fiselier     {
595a83710eSEric Fiselier         assert(!alloc_last::allocator_constructed);
605a83710eSEric Fiselier         std::tuple<alloc_last> t(std::allocator_arg, A1<int>(5));
615a83710eSEric Fiselier         assert(alloc_last::allocator_constructed);
625a83710eSEric Fiselier         assert(std::get<0>(t) == alloc_last());
635a83710eSEric Fiselier     }
645a83710eSEric Fiselier     {
655a83710eSEric Fiselier         alloc_first::allocator_constructed = false;
665a83710eSEric Fiselier         std::tuple<DefaultOnly, alloc_first> t(std::allocator_arg, A1<int>(5));
675a83710eSEric Fiselier         assert(std::get<0>(t) == DefaultOnly());
685a83710eSEric Fiselier         assert(alloc_first::allocator_constructed);
695a83710eSEric Fiselier         assert(std::get<1>(t) == alloc_first());
705a83710eSEric Fiselier     }
715a83710eSEric Fiselier     {
725a83710eSEric Fiselier         alloc_first::allocator_constructed = false;
735a83710eSEric Fiselier         alloc_last::allocator_constructed = false;
745a83710eSEric Fiselier         std::tuple<DefaultOnly, alloc_first, alloc_last> t(std::allocator_arg,
755a83710eSEric Fiselier                                                            A1<int>(5));
765a83710eSEric Fiselier         assert(std::get<0>(t) == DefaultOnly());
775a83710eSEric Fiselier         assert(alloc_first::allocator_constructed);
785a83710eSEric Fiselier         assert(std::get<1>(t) == alloc_first());
795a83710eSEric Fiselier         assert(alloc_last::allocator_constructed);
805a83710eSEric Fiselier         assert(std::get<2>(t) == alloc_last());
815a83710eSEric Fiselier     }
825a83710eSEric Fiselier     {
835a83710eSEric Fiselier         alloc_first::allocator_constructed = false;
845a83710eSEric Fiselier         alloc_last::allocator_constructed = false;
855a83710eSEric Fiselier         std::tuple<DefaultOnly, alloc_first, alloc_last> t(std::allocator_arg,
865a83710eSEric Fiselier                                                            A2<int>(5));
875a83710eSEric Fiselier         assert(std::get<0>(t) == DefaultOnly());
885a83710eSEric Fiselier         assert(!alloc_first::allocator_constructed);
895a83710eSEric Fiselier         assert(std::get<1>(t) == alloc_first());
905a83710eSEric Fiselier         assert(!alloc_last::allocator_constructed);
915a83710eSEric Fiselier         assert(std::get<2>(t) == alloc_last());
925a83710eSEric Fiselier     }
9390fb2bafSEric Fiselier     {
94*a3ab5120SLouis Dionne         // Test that we can use a tag derived from allocator_arg_t
95*a3ab5120SLouis Dionne         struct DerivedFromAllocatorArgT : std::allocator_arg_t { };
96*a3ab5120SLouis Dionne         DerivedFromAllocatorArgT derived;
97*a3ab5120SLouis Dionne         std::tuple<> t1(derived, A1<int>());
98*a3ab5120SLouis Dionne         std::tuple<int> t2(derived, A1<int>());
99*a3ab5120SLouis Dionne         std::tuple<int, int> t3(derived, A1<int>());
100*a3ab5120SLouis Dionne     }
101*a3ab5120SLouis Dionne     {
10290fb2bafSEric Fiselier         // Test that the uses-allocator default constructor does not evaluate
103881d8613SStephan T. Lavavej         // its SFINAE when it otherwise shouldn't be selected. Do this by
10490fb2bafSEric Fiselier         // using 'NonDefaultConstructible' which will cause a compile error
10590fb2bafSEric Fiselier         // if std::is_default_constructible is evaluated on it.
10690fb2bafSEric Fiselier         using T = NonDefaultConstructible<>;
10790fb2bafSEric Fiselier         T v(42);
10890fb2bafSEric Fiselier         std::tuple<T, T> t(v, v);
109346bd6a2SBenjamin Kramer         (void)t;
11090fb2bafSEric Fiselier         std::tuple<T, T> t2(42, 42);
111346bd6a2SBenjamin Kramer         (void)t2;
11290fb2bafSEric Fiselier     }
1132df59c50SJF Bastien 
1142df59c50SJF Bastien   return 0;
1155a83710eSEric Fiselier }
116