std::result_of

判断值类型的方法有三种:
通过decltype
通过declval
通过result_of

 

#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;

    system("pause");
}

 

posted @ 2019-06-26 23:30  osbreak  阅读(922)  评论(0编辑  收藏  举报