xref: /llvm-project/libcxx/test/std/experimental/memory/memory.observer.ptr/hash.pass.cpp (revision 09e3a360581dc36d0820d3fb6da9bd7cfed87b5d)
17a62bee6SZoe Carver //===----------------------------------------------------------------------===//
27a62bee6SZoe Carver //
37a62bee6SZoe Carver // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47a62bee6SZoe Carver // See https://llvm.org/LICENSE.txt for license information.
57a62bee6SZoe Carver // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67a62bee6SZoe Carver //
77a62bee6SZoe Carver //===----------------------------------------------------------------------===//
87a62bee6SZoe Carver 
97a62bee6SZoe Carver // UNSUPPORTED: c++03, c++11, c++14
107a62bee6SZoe Carver // REQUIRES: c++experimental
117a62bee6SZoe Carver 
127a62bee6SZoe Carver // <experimental/memory>
137a62bee6SZoe Carver 
147a62bee6SZoe Carver // observer_ptr
157a62bee6SZoe Carver //
167a62bee6SZoe Carver // template <class T> struct hash<std::experimental::observer_ptr<T>>;
177a62bee6SZoe Carver 
187a62bee6SZoe Carver #include <experimental/memory>
197a62bee6SZoe Carver #include <cassert>
20*09e3a360SLouis Dionne #include <functional>
217a62bee6SZoe Carver 
227a62bee6SZoe Carver #include "poisoned_hash_helper.h"
237a62bee6SZoe Carver 
247a62bee6SZoe Carver template <class T, class Object = T>
257a62bee6SZoe Carver void test_hash() {
267a62bee6SZoe Carver   {
277a62bee6SZoe Carver     using Ptr = std::experimental::observer_ptr<T>;
287a62bee6SZoe Carver     Object obj;
297a62bee6SZoe Carver     Ptr ptr(&obj);
307a62bee6SZoe Carver 
317a62bee6SZoe Carver     std::hash<std::experimental::observer_ptr<T>> f;
327a62bee6SZoe Carver     std::size_t h = f(ptr);
337a62bee6SZoe Carver 
347a62bee6SZoe Carver     assert(h == std::hash<T*>()(&obj));
357a62bee6SZoe Carver   }
367a62bee6SZoe Carver 
3733325524SLouis Dionne   test_hash_enabled<std::experimental::observer_ptr<T>>();
387a62bee6SZoe Carver }
397a62bee6SZoe Carver 
407a62bee6SZoe Carver struct Bar {};
417a62bee6SZoe Carver 
427a62bee6SZoe Carver void test() {
437a62bee6SZoe Carver   test_hash<void, int>();
447a62bee6SZoe Carver   test_hash<int>();
457a62bee6SZoe Carver   test_hash<Bar>();
467a62bee6SZoe Carver }
477a62bee6SZoe Carver 
487a62bee6SZoe Carver int main(int, char**) {
497a62bee6SZoe Carver   // Note: This isn't constexpr friendly in the spec!
507a62bee6SZoe Carver   test();
517a62bee6SZoe Carver 
527a62bee6SZoe Carver   return 0;
537a62bee6SZoe Carver }
54