// { dg-do run { target c++17 } }
// Copyright (C) 2016-2025 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
// .
#include
#include
class S{}; // No hash specialization
template
auto f(int) -> decltype(std::hash>(), std::true_type());
template
auto f(...) -> decltype(std::false_type());
static_assert(!decltype(f(0))::value, "");
template
constexpr bool hashable()
{ return std::is_invocable_v&, const T&>; }
static_assert(!hashable>());
static_assert(!hashable>());
static_assert(hashable>());
static_assert(hashable>());
int main()
{
int x = 42;
std::optional x2 = 42;
VERIFY(std::hash()(x) == std::hash>()(x2));
// PR libstdc++/82262
std::optional x3 = x2;
VERIFY(std::hash()(x) == std::hash>()(x3));
}
// Check for presence/absence of nested types.
template using res_type = typename std::hash::result_type;
template using arg_type = typename std::hash::argument_type;
template
constexpr bool has_res_type = false;
template
constexpr bool has_res_type>> = true;
template
constexpr bool has_arg_type = false;
template
constexpr bool has_arg_type>> = true;
template
constexpr bool has_no_types
= ! has_res_type> && ! has_arg_type>;
#if __cplusplus >= 202002L
// Nested types result_type and argument_type are not present in C++20
static_assert( has_no_types );
static_assert( has_no_types );
#else
// Nested types result_type and argument_type are deprecated in C++17.
using R1 = std::hash>::result_type; // { dg-warning "deprecated" "" { target c++17_only } }
using A1 = std::hash>::argument_type; // { dg-warning "deprecated" "" { target c++17_only } }
using R2 = std::hash>::result_type; // { dg-warning "deprecated" "" { target c++17_only } }
using A2 = std::hash>::argument_type; // { dg-warning "deprecated" "" { target c++17_only } }
#endif
// Disabled specializations do not have the nested types.
static_assert( has_no_types );