| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <functional> |
| 10 |
|
| 11 |
|
| 12 |
// In C++17, the function adapters mem_fun/mem_fun_ref, etc have been removed. |
| 13 |
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX17_REMOVED_BINDERS |
| 14 |
// is defined before including <functional>, then they will be restored. |
| 15 |
|
| 16 |
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS |
| 17 |
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS |
| 18 |
|
| 19 |
#include <functional> |
| 20 |
#include <cassert> |
| 21 |
#include <type_traits> |
| 22 |
|
| 23 |
#include "test_macros.h" |
| 24 |
|
| 25 |
int identity(int v) { return v; } |
| 26 |
int sum(int a, int b) { return a + b; } |
| 27 |
|
| 28 |
struct Foo { |
| 29 |
int zero() { return 0; } |
| 30 |
int zero_const() const { return 1; } |
| 31 |
|
| 32 |
int identity(int v) const { return v; } |
| 33 |
int sum(int a, int b) const { return a + b; } |
| 34 |
}; |
| 35 |
|
| 36 |
int main(int, char**) |
| 37 |
{ |
| 38 |
typedef std::pointer_to_unary_function<int, int> PUF; |
| 39 |
typedef std::pointer_to_binary_function<int, int, int> PBF; |
| 40 |
|
| 41 |
static_assert( |
| 42 |
(std::is_same<PUF, decltype((std::ptr_fun<int, int>(identity)))>::value), |
| 43 |
""); |
| 44 |
static_assert( |
| 45 |
(std::is_same<PBF, decltype((std::ptr_fun<int, int, int>(sum)))>::value), |
| 46 |
""); |
| 47 |
|
| 48 |
assert((std::ptr_fun<int, int>(identity)(4) == 4)); |
| 49 |
assert((std::ptr_fun<int, int, int>(sum)(4, 5) == 9)); |
| 50 |
|
| 51 |
Foo f; |
| 52 |
assert((std::mem_fn(&Foo::identity)(f, 5) == 5)); |
| 53 |
assert((std::mem_fn(&Foo::sum)(f, 5, 6) == 11)); |
| 54 |
|
| 55 |
typedef std::mem_fun_ref_t<int, Foo> MFR; |
| 56 |
typedef std::const_mem_fun_ref_t<int, Foo> CMFR; |
| 57 |
|
| 58 |
static_assert( |
| 59 |
(std::is_same<MFR, decltype((std::mem_fun_ref(&Foo::zero)))>::value), ""); |
| 60 |
static_assert((std::is_same<CMFR, decltype((std::mem_fun_ref( |
| 61 |
&Foo::zero_const)))>::value), |
| 62 |
""); |
| 63 |
|
| 64 |
assert((std::mem_fun_ref(&Foo::zero)(f) == 0)); |
| 65 |
assert((std::mem_fun_ref(&Foo::identity)(f, 5) == 5)); |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |