<type_traits>

類模板
<type_traits>

std::result_of

template <class Fn, class... ArgTypes> struct result_of<Fn(ArgTypes...)>;
呼叫結果
獲取對某個呼叫的*結果型別*Fn引數型別列表為ArgTypes.

結果型別被別名為成員型別result_of::type並在以下任一情況定義

  • 如果 Fn 是一個*函式*或函式物件型別,並且接收 ArgTypes 作為引數。
  • 如果 Fn 是一個*非靜態成員函式的指標*,並且 ArgTypes 中的第一個型別是該成員所屬的類(或其引用、或其派生型別的引用、或其指標),而 ArgTypes 中的其餘型別描述了它的引數。
  • 如果 Fn 是一個*非靜態資料成員的指標*,並且 ArgTypes 是一個與該成員所屬類匹配的單一型別(或其引用、或其派生型別的引用、或其指標)。在這種情況下,成員 type 是該資料成員的型別。
在所有其他情況下,成員 type 未定義。

模板引數

Fn
一個*可呼叫型別*(即函式物件型別或成員指標),或函式的引用,或可呼叫型別的引用。
ArgTypes...
型別列表,順序與呼叫中的一致。
請注意,模板引數不是用逗號分隔的,而是以函式形式給出。

成員型別

成員型別定義
型別的呼叫結果型別Fn具有型別引數列表的ArgTypes.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// result_of example
#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;
}

輸出
typedefs of int:
A: true
B: true
C: true
D: true


另見