summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/scoped_allocator/outermost.cc
blob: af4d29443f5556a4af6baf0c959f762a9d42d72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2016-2022 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
// <http://www.gnu.org/licenses/>.

// { dg-do compile { target c++11 } }

#include <scoped_allocator>

template<typename T>
struct alloc
{
  using value_type = T;
  alloc() = default;
  template<typename U>
    alloc(alloc<U>) { }
  T* allocate(std::size_t);
  void deallocate(T*, std::size_t);
};

template<typename T, typename U>
  bool operator==(alloc<T>, alloc<U>) { return true; }

template<typename T, typename U>
  bool operator!=(alloc<T>, alloc<U>) { return false; }

struct X
{
  using allocator_type = alloc<int>;
  X(const allocator_type&);
};

template<typename A>
struct nested_alloc : A
{
  nested_alloc() = default;
  template<typename U>
    nested_alloc(nested_alloc<U>) { }

  // Need to customize rebind, otherwise nested_alloc<alloc<T>> gets rebound
  // to nested_alloc<U>.
  template<typename U>
    struct rebind
    {
      using other = typename std::allocator_traits<A>::template rebind_alloc<U>;
    };

  A& outer_allocator() { return *this; }

  template<typename U, typename... Args>
    void construct(U*, Args&&...)
    {
      static_assert(!std::is_same<U, X>::value,
          "OUTERMOST should recurse and use alloc<int> to construct X");
    }
};

template<typename T, typename U>
  bool operator==(nested_alloc<T> l, nested_alloc<U> r)
  { return l.outer_allocator() == r.outer_allocator(); }

template<typename T, typename U>
  bool operator!=(nested_alloc<T> l, nested_alloc<U> r)
  { return !(l == r); }

template<typename A>
  using scoped_alloc = std::scoped_allocator_adaptor<A>;

void
test01()
{
  scoped_alloc<nested_alloc<alloc<int>>> a;
  alignas(X) char buf[sizeof(X)];
  X* p = (X*)buf;
  // Test that OUTERMOST is recursive and doesn't just unwrap one level:
  a.construct(p);
}

void
test02()
{
  scoped_alloc<scoped_alloc<nested_alloc<alloc<int>>>> a;
  alignas(X) char buf[sizeof(X)];
  X* p = (X*)buf;
  // Test that OUTERMOST is recursive and doesn't just unwrap one level:
  a.construct(p);
}