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