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

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

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

    Answer:

    You could use decltype and std::declval like the toy example below:

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

    LIVE DEMO

提交回复
热议问题