C++11 result_of 学习

简介

std::result_of<Function(Args...)>::type 简而言之就是推断
Function(Args...) 这个函数的返回值类型,用在模板函数中

参考链接

https://blog.csdn.net/zhouguoqionghai/article/details/45789295
引用

result_of() 函数使用到了模板元编程技术,对于 F(Arg1, Arg2... Arg3) ,其中F 是一个可调用对象(函数指针,函数引用, 成员函数指针,或者是函数对象)f 的类型,Arg是其参数arg1, arg2 的类型。 那么reslut_of <Func(Arg1, Arg2... Arg3)> :: type 表示的也就是 f(arg1,arg2 ... argn)的返回值的类型。

http://www.cplusplus.com/reference/type_traits/result_of/ (C++官网的知识链接)
引用

Obtains the result type of a call to Fn with arguments of the types listed in ArgTypes.
获得函数Fn在所带参数的返回值类型

code

#include <iostream>
#include <type_traits>

int fn(int) {return int();}                            // function
typedef int(&fn_ref)(int);                             // function reference
typedef int(*fn_ptr)(int);                             // function pointer
struct fn_class { int operator()(int i){return i;} };  // function-like class

int main() {
  typedef std::result_of<decltype(fn)&(int)>::type A;  // int
  typedef std::result_of<fn_ref(int)>::type B;         // int
  typedef std::result_of<fn_ptr(int)>::type C;         // int
  typedef std::result_of<fn_class(int)>::type D;       // int

  std::cout << std::boolalpha;
  std::cout << "typedefs of int:" << std::endl;

  std::cout << "A: " << std::is_same<int,A>::value << std::endl;
  std::cout << "B: " << std::is_same<int,B>::value << std::endl;
  std::cout << "C: " << std::is_same<int,C>::value << std::endl;
  std::cout << "D: " << std::is_same<int,D>::value << std::endl;

  return 0;
}
Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
原文地址:https://www.cnblogs.com/eat-too-much/p/14441330.html