libstdc++
invoke.h
Go to the documentation of this file.
00001 // Implementation of INVOKE -*- C++ -*-
00002 
00003 // Copyright (C) 2016-2017 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/bits/invoke.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{functional}
00028  */
00029 
00030 #ifndef _GLIBCXX_INVOKE_H
00031 #define _GLIBCXX_INVOKE_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #if __cplusplus < 201103L
00036 # include <bits/c++0x_warning.h>
00037 #else
00038 
00039 #include <type_traits>
00040 
00041 namespace std _GLIBCXX_VISIBILITY(default)
00042 {
00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00044 
00045   /**
00046    *  @addtogroup utilities
00047    *  @{
00048    */
00049 
00050   // Used by __invoke_impl instead of std::forward<_Tp> so that a
00051   // reference_wrapper is converted to an lvalue-reference.
00052   template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
00053     constexpr _Up&&
00054     __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
00055     { return static_cast<_Up&&>(__t); }
00056 
00057   template<typename _Res, typename _Fn, typename... _Args>
00058     constexpr _Res
00059     __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
00060     { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
00061 
00062   template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
00063     constexpr _Res
00064     __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
00065                   _Args&&... __args)
00066     { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
00067 
00068   template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
00069     constexpr _Res
00070     __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
00071                   _Args&&... __args)
00072     {
00073       return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
00074     }
00075 
00076   template<typename _Res, typename _MemPtr, typename _Tp>
00077     constexpr _Res
00078     __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
00079     { return __invfwd<_Tp>(__t).*__f; }
00080 
00081   template<typename _Res, typename _MemPtr, typename _Tp>
00082     constexpr _Res
00083     __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
00084     { return (*std::forward<_Tp>(__t)).*__f; }
00085 
00086   /// Invoke a callable object.
00087   template<typename _Callable, typename... _Args>
00088     constexpr typename __invoke_result<_Callable, _Args...>::type
00089     __invoke(_Callable&& __fn, _Args&&... __args)
00090     noexcept(__is_nothrow_invocable<_Callable, _Args...>::value)
00091     {
00092       using __result = __invoke_result<_Callable, _Args...>;
00093       using __type = typename __result::type;
00094       using __tag = typename __result::__invoke_type;
00095       return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
00096                                         std::forward<_Args>(__args)...);
00097     }
00098 
00099 _GLIBCXX_END_NAMESPACE_VERSION
00100 } // namespace std
00101 
00102 #endif // C++11
00103 
00104 #endif // _GLIBCXX_INVOKE_H