Get the type of the return value in C++

前端 未结 2 499
我寻月下人不归
我寻月下人不归 2021-01-04 20:56

Suppose we have a function f which returns a value of some unknown type (let\'s call it T) and takes a value of the type T as an argum

2条回答
  •  长情又很酷
    2021-01-04 21:17

    template 
    struct return_type;
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    struct return_type { using type = R; };
    
    template 
    using return_type_t = typename return_type::type;
    

    Test:

    #include 
    
    struct Functor
    {
        int operator()(int i, int j) { return i + j; }
    };
    
    template 
    struct A
    {
        using T = return_type_t;
    
        T some_function(T some_arg) { return some_arg; }
    };
    
    int main()
    {
        A a;
        static_assert(std::is_same::value, "!");
    }
    

    DEMO

提交回复
热议问题