Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++14) |
Defined in header <type_traits> | ||
template<class> class result_of;// not defined | (1) | (since C++11) (deprecated in C++17) (removed in C++20) |
template<class F,class...ArgTypes> class invoke_result; | (2) | (since C++17) |
Deduces the return type of anINVOKE
expression at compile time.
| (since C++11) (until C++14) |
| (since C++14) |
If the program adds specializations for any of the templates described on this page, the behavior is undefined.
Contents |
Member type | Definition |
type | the return type of theCallable typeF if invoked with the argumentsArgTypes... .Only defined if F can be called with the argumentsArgTypes... in unevaluated context.(since C++14) |
template<class T> using result_of_t=typename result_of<T>::type; | (1) | (since C++14) (deprecated in C++17) (removed in C++20) |
template<class F,class...ArgTypes> using invoke_result_t=typename invoke_result<F, ArgTypes...>::type; | (2) | (since C++17) |
namespace detail{template<class T>struct is_reference_wrapper:std::false_type{};template<class U>struct is_reference_wrapper<std::reference_wrapper<U>>:std::true_type{}; template<class T>struct invoke_impl{template<class F,class...Args>staticauto call(F&& f, Args&&...args)-> decltype(std::forward<F>(f)(std::forward<Args>(args)...));}; template<class B,class MT>struct invoke_impl<MT B::*>{template<class T,class Td=typenamestd::decay<T>::type,class=typenamestd::enable_if<std::is_base_of<B, Td>::value>::type>staticauto get(T&& t)-> T&&; template<class T,class Td=typenamestd::decay<T>::type,class=typenamestd::enable_if<is_reference_wrapper<Td>::value>::type>staticauto get(T&& t)-> decltype(t.get()); template<class T,class Td=typenamestd::decay<T>::type,class=typenamestd::enable_if<!std::is_base_of<B, Td>::value>::type,class=typenamestd::enable_if<!is_reference_wrapper<Td>::value>::type>staticauto get(T&& t)-> decltype(*std::forward<T>(t)); template<class T,class...Args,class MT1,class=typenamestd::enable_if<std::is_function<MT1>::value>::type>staticauto call(MT1 B::*pmf, T&& t, Args&&...args)-> decltype((invoke_impl::get(std::forward<T>(t)).*pmf)(std::forward<Args>(args)...)); template<class T>staticauto call(MT B::*pmd, T&& t)-> decltype(invoke_impl::get(std::forward<T>(t)).*pmd);}; template<class F,class...Args,class Fd=typenamestd::decay<F>::type>auto INVOKE(F&& f, Args&&...args)-> decltype(invoke_impl<Fd>::call(std::forward<F>(f),std::forward<Args>(args)...));}// namespace detail // Minimal C++11 implementation:template<class>struct result_of;template<class F,class...ArgTypes>struct result_of<F(ArgTypes...)>{using type= decltype(detail::INVOKE(std::declval<F>(),std::declval<ArgTypes>()...));}; // Conforming C++14 implementation (is also a valid C++11 implementation):namespace detail{template<typename AlwaysVoid,typename,typename...>struct invoke_result{};template<typename F,typename...Args>struct invoke_result< decltype(void(detail::INVOKE(std::declval<F>(),std::declval<Args>()...))), F, Args...>{using type= decltype(detail::INVOKE(std::declval<F>(),std::declval<Args>()...));};}// namespace detail template<class>struct result_of;template<class F,class...ArgTypes>struct result_of<F(ArgTypes...)>: detail::invoke_result<void, F, ArgTypes...>{}; template<class F,class...ArgTypes>struct invoke_result: detail::invoke_result<void, F, ArgTypes...>{};
As formulated in C++11, the behavior ofstd::result_of
is undefined whenINVOKE(std::declval<F>(), std::declval<ArgTypes>()...)
is ill-formed (e.g. when F is not a callable type at all). C++14 changes that to aSFINAE (when F is not callable,std::result_of<F(ArgTypes...)>
simply doesn't have thetype
member).
The motivation behindstd::result_of
is to determine the result of invoking aCallable, in particular if that result type is different for different sets of arguments.
F(Args...) is a function type withArgs...
being the argument types andF
being the return type. As such,std::result_of
suffers from several quirks that led to its deprecation in favor ofstd::invoke_result
in C++17:
F
cannot be a function type or an array type (but can be a reference to them);Args
has type "array ofT
" or a function typeT
, it is automatically adjusted toT*
;F
nor any ofArgs...
can be an abstract class type;Args...
has a top-level cv-qualifier, it is discarded;Args...
may be of typevoid.To avoid these quirks,result_of
is often used with reference types asF
andArgs...
. For example:
template<class F,class...Args>std::result_of_t<F&&(Args&&...)>// instead of std::result_of_t<F(Args...)>, which is wrong my_invoke(F&& f, Args&&...args){/* implementation */}
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_result_of_sfinae | 201210L | (C++14) | std::result_of andSFINAE |
__cpp_lib_is_invocable | 201703L | (C++17) | std::is_invocable,std::invoke_result |
#include <iostream>#include <type_traits> struct S{double operator()(char,int&);float operator()(int){return1.0;}}; template<class T>typename std::result_of<T(int)>::type f(T& t){std::cout<<"overload of f for callable T\n";return t(0);} template<class T,class U>int f(U u){std::cout<<"overload of f for non-callable T\n";return u;} int main(){// the result of invoking S with char and int& arguments is double std::result_of<S(char,int&)>::type d=3.14;// d has type double static_assert(std::is_same<decltype(d),double>::value,""); // std::invoke_result uses different syntax (no parentheses) std::invoke_result<S,char,int&>::type b=3.14; static_assert(std::is_same<decltype(b),double>::value,""); // the result of invoking S with int argument is float std::result_of<S(int)>::type x=3.14;// x has type float static_assert(std::is_same<decltype(x),float>::value,""); // result_of can be used with a pointer to member function as followsstruct C{double Func(char,int&);}; std::result_of<decltype(&C::Func)(C,char,int&)>::type g=3.14; static_assert(std::is_same<decltype(g),double>::value,""); f<C>(1);// may fail to compile in C++11; calls the non-callable overload in C++14}
Output:
overload of f for non-callable T
(C++17)(C++23) | invokes anyCallable object with given argumentsand possibility to specify return type(since C++23) (function template)[edit] |
checks if a type can be invoked (as if bystd::invoke) with the given argument types (class template)[edit] | |
(C++11) | obtains a reference to an object of the template type argument for use in an unevaluated context (function template)[edit] |