How can I determine the return type of a C++11 member function

后端 未结 4 1322
心在旅途
心在旅途 2021-02-19 20:39

I am trying to determine the return type of a various C++ member functions. I understand that decltype and std::declval can be used to do this, but I am having problems with the

4条回答
  •  悲哀的现实
    2021-02-19 21:05

    I've tried various declarations such as decltype(std::declval(TestCBClass::testStaticMethod))

    You don't have to use std::declval and pass actual arguments, not even their types, just to know what is the return type of a static/non-static member function. Instead, you can write your own trait to know what is the return type of a given function:

    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 
    using return_type_t = typename return_type::type;
    
    ...
    
    TestCBClass t;
    
    std::future> a =
            std::async(&TestCBClass::testCBArgRet, t, 1);
    
    std::future> b =
            std::async(&TestCBClass::testCBEmpty, t);
    
    std::future> c =
            std::async(&TestCBClass::testCBEmptyStatic);
    

    DEMO

提交回复
热议问题