105 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <functional>10 11// reference_wrapper12 13// has weak result type14 15// REQUIRES: c++03 || c++11 || c++14 || c++1716 17// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS18 19#include <functional>20#include <type_traits>21 22#include "test_macros.h"23 24template <class Arg, class Result>25struct my_unary_function26{ // std::unary_function was removed in C++1727 typedef Arg argument_type;28 typedef Result result_type;29};30 31template <class Arg1, class Arg2, class Result>32struct my_binary_function33{ // std::binary_function was removed in C++1734 typedef Arg1 first_argument_type;35 typedef Arg2 second_argument_type;36 typedef Result result_type;37};38 39class functor140 : public my_unary_function<int, char>41{42};43 44class functor245 : public my_binary_function<char, int, double>46{47};48 49class functor350 : public my_unary_function<char, int>,51 public my_binary_function<char, int, double>52{53public:54 typedef float result_type;55};56 57class functor458 : public my_unary_function<char, int>,59 public my_binary_function<char, int, double>60{61public:62};63 64class C {};65 66template <class T>67struct has_result_type68{69private:70 struct two {char _; char __;};71 template <class U> static two test(...);72 template <class U> static char test(typename U::result_type* = 0);73public:74 static const bool value = sizeof(test<T>(0)) == 1;75};76 77int main(int, char**)78{79 static_assert((std::is_same<std::reference_wrapper<functor1>::result_type,80 char>::value), "");81 static_assert((std::is_same<std::reference_wrapper<functor2>::result_type,82 double>::value), "");83 static_assert((std::is_same<std::reference_wrapper<functor3>::result_type,84 float>::value), "");85 static_assert((std::is_same<std::reference_wrapper<void()>::result_type,86 void>::value), "");87 static_assert((std::is_same<std::reference_wrapper<int*(double*)>::result_type,88 int*>::value), "");89 static_assert((std::is_same<std::reference_wrapper<void(*)()>::result_type,90 void>::value), "");91 static_assert((std::is_same<std::reference_wrapper<int*(*)(double*)>::result_type,92 int*>::value), "");93 static_assert((std::is_same<std::reference_wrapper<int*(C::*)(double*)>::result_type,94 int*>::value), "");95 static_assert((std::is_same<std::reference_wrapper<int (C::*)(double*) const volatile>::result_type,96 int>::value), "");97 static_assert((std::is_same<std::reference_wrapper<C()>::result_type,98 C>::value), "");99 static_assert(has_result_type<std::reference_wrapper<functor3> >::value, "");100 static_assert(!has_result_type<std::reference_wrapper<functor4> >::value, "");101 static_assert(!has_result_type<std::reference_wrapper<C> >::value, "");102 103 return 0;104}105