]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/allocator_traits/members/allocate_hint.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / allocator_traits / members / allocate_hint.cc
CommitLineData
be58e01d 1// { dg-do run { target c++11 } }
2414d57e 2
fbd26352 3// Copyright (C) 2011-2019 Free Software Foundation, Inc.
2414d57e 4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
19
20#include <memory>
21#include <cstddef>
22#include <testsuite_hooks.h>
23
24struct X { };
25
26template<typename T>
27struct hintable_allocator
28{
29 typedef T value_type;
30 struct const_void_pointer { };
31 typedef unsigned size_type;
32
33 hintable_allocator() : called(false) { }
34
35 bool called;
36
37 // this is the overload that should get called:
38 T* allocate(size_type n, const_void_pointer) { called = true; return 0; }
39
40 // none of these should get called:
41 T* allocate(size_type n);
42 T* allocate(size_type n, void*);
43 T* allocate(size_type n, const void*);
44 T* allocate(size_type n, const_void_pointer) const;
45};
46
47void test01()
48{
2414d57e 49 typedef std::allocator_traits<hintable_allocator<X>> traits_type;
50 traits_type::allocator_type a;
51 traits_type::const_void_pointer v;
ec54e51f 52 X* p __attribute__((unused)) = traits_type::allocate(a, 1, v);
2414d57e 53 VERIFY( a.called );
54}
55
56template<typename T>
57struct unhintable_allocator
58{
59 typedef T value_type;
60 typedef unsigned size_type;
61
62 unhintable_allocator() : called(false) { }
63
64 bool called;
65
66 // this is the overload that should get called:
67 T* allocate(size_type n) { called = true; return 0; }
68
69 // this should not get called:
70 T* allocate(size_type n, void*);
71};
72
73void test02()
74{
2414d57e 75 typedef std::allocator_traits<unhintable_allocator<X>> traits_type;
76 traits_type::allocator_type a;
77 traits_type::const_void_pointer v;
ec54e51f 78 X* p __attribute__((unused)) = traits_type::allocate(a, 1, v);
2414d57e 79 VERIFY( a.called );
80}
81
82int main()
83{
84 test01();
85 test02();
86}