summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/unique_ptr/hash/90388.cc
blob: 7904205a8ce9e3fec40c6d7c590edf3d421eb0f8 (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
// Copyright (C) 2019-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 run { target c++11 } }

#include <memory>

template<typename Func, typename Arg, typename = void>
  struct is_callable
  : std::false_type
  { };

template<typename Func, typename Arg>
  struct is_callable<Func, Arg,
    decltype((void)(std::declval<Func&>()(std::declval<Arg>())))>
  : std::true_type
  { };

void
test01()
{
  struct D {
    struct pointer { };
    void operator()(pointer) const noexcept { }
  };
  static_assert( !is_callable<std::hash<D::pointer>&, D::pointer>::value );

  using UP = std::unique_ptr<int, D>;
  // [unord.hash]
  // Disabled specializations of hash are not function object types
  static_assert( !is_callable<std::hash<UP>&, UP>::value );
  static_assert( !is_callable<std::hash<UP>&, UP&>::value );
  static_assert( !is_callable<std::hash<UP>&, const UP&>::value );
}

struct D {
  struct pointer { };
  void operator()(pointer) const noexcept { }
};

bool operator==(D::pointer, std::nullptr_t) { return false; }
bool operator!=(D::pointer, std::nullptr_t) { return true; }

namespace std {
  template<> struct hash<D::pointer> {
    size_t operator()(D::pointer) const { throw 1; }
  };
}

void
test02()
{
  using UP = std::unique_ptr<int, D>;
  UP p;
  std::hash<UP> h;
  try {
    // [util.smartptr.hash]
    // The member functions are not guaranteed to be noexcept.
    h(p);
    throw "should not reach here";
  } catch (int) {
    // Should catch exception here, rather than terminating.
  }

  // Should still be noexcept if the underlying hash object is:
  using UP2 = std::unique_ptr<int>;
  UP2 p2;
  std::hash<UP2> h2;
  static_assert( noexcept(h2(p2)), "operator() is noexcept" );
}

int
main()
{
  test02();
}