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

I am trying to determine the return type of various C ++ member functions. I understand that you can use decltype and std :: declval for this, but I am having problems with the syntax and searching for useful examples. The following is an example TestCBClass example of a silent class that contains the static and normal member functions - with and without arguments and return types. Depending on the method in question, I would like to be able to declare a vector of return types from each of the various methods.

In my application, these methods are callbacks for std::async , and I need the vector std::future<return types> . I tried various declarations like decltype(std::declval(TestCBClass::testStaticMethod)) (I'm not sure what I need & before the method name). This syntax is incorrect - and, of course, it does not compile, but I think its approach should be used.

 class TestCBClass { public: TestCBClass(const int& rValue = 1) : mValue(rValue) { std::cout << "~TestCBClass()" << std::endl; } virtual ~TestCBClass() { std::cout << "~TestCBClass()" << std::endl; } void testCBEmpty(void) { std::cout << "testCBEmpty()" << std::endl; } int testCBArgRet(const int& rArg) { std::cout << "testCBArgRet(" << rArg << ")" << std::endl; mValue = rArg; } static void testCBEmptyStatic(void) { std::cout << "testCBEmptyStatic()" << std::endl; } static void cbArgRetStatic(const SLDBConfigParams& rParams) { std::lock_guard<std::mutex> lock(gMutexGuard); std::cout << rParams.mPriority << std::endl; } static std::string testStaticMethod(const PriorityLevel& rPrty) { return "this is a silly return string"; } private: int mValue; }; 
+5
source share
3 answers

You can also use std::result_of and decltype if you prefer to list argument types rather than corresponding dummy values, for example:

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

DEMO is here.

+5
source

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

Answer:

You can use decltype and std::declval as an example of toys below:

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

Live demo

+4
source

I tried various declarations like decltype (std :: declval (TestCBClass :: testStaticMethod))

You do not need to use std::declval and pass the actual arguments, even their types, to find out what the return type of the static / non-static member function is. Instead, you can write your own attribute to find out what the return type of this function is:

 template <typename T> struct return_type; template <typename R, typename... Args> struct return_type<R(*)(Args...)> { using type = R; }; template <typename R, typename C, typename... Args> struct return_type<R(C::*)(Args...)> { using type = R; }; template <typename R, typename C, typename... Args> struct return_type<R(C::*)(Args...) const> { using type = R; }; template <typename R, typename C, typename... Args> struct return_type<R(C::*)(Args...) volatile> { using type = R; }; template <typename R, typename C, typename... Args> struct return_type<R(C::*)(Args...) const volatile> { using type = R; }; template <typename T> using return_type_t = typename return_type<T>::type; ... TestCBClass t; std::future<return_type_t<decltype(&TestCBClass::testCBArgRet)>> a = std::async(&TestCBClass::testCBArgRet, t, 1); std::future<return_type_t<decltype(&TestCBClass::testCBEmpty)>> b = std::async(&TestCBClass::testCBEmpty, t); std::future<return_type_t<decltype(&TestCBClass::testCBEmptyStatic)>> c = std::async(&TestCBClass::testCBEmptyStatic); 

Demo

+4
source

Source: https://habr.com/ru/post/1203647/


All Articles