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

后端 未结 4 1167
心在旅途
心在旅途 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:07

    You may also use std::result_of and decltype, if you prefer to list arguments types rather than the corresponding dummy values, like this:

    #include 
    #include 
    #include 
    
    struct foo {
      int    memfun1(int a) const { return a;   }
      double memfun2(double b) const { return b; }
    };
    
    int main() {
      std::result_of::type i = 10;
      std::cout << i << std::endl;
      std::result_of::type d = 12.9;
      std::cout << d << std::endl;
    }
    

    DEMO here.

提交回复
热议问题