Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 syntax and finding useful examples. The TestCBClass below shows an example of a dumb class that contains a mixture static and normal member functions - with & 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 a vector of std::future<return types>. I've tried various declarations such as decltype(std::declval(TestCBClass::testStaticMethod)) (I'm not sure if I need an & before the method name). This syntax is incorrect - and of course it does not compile, but I think its the approach that 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;
};
like image 631
johnco3 Avatar asked Sep 29 '14 19:09

johnco3


People also ask

What is the return type of a member function?

Member Function Return Types. A public member function must never return a non-const reference or pointer to member data. A public member function must never return a non-const reference or pointer to data outside an object, unless the object shares the data with other objects.

How do you determine the return type of a function in C++?

using ReturnTypeOfFoo = decltype( getRetType(&foo) ); Observe that getRetType() is only declared and not defined because is called only a decltype() , so only the returned type is relevant.

Can auto be a return type?

In C++14, you can just use auto as a return type.

What does Decltype function do in C++?

The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a function template whose return type depends on the types of its template arguments.


2 Answers

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

like image 88
Krizz Avatar answered Sep 18 '22 09:09

Krizz


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

like image 37
101010 Avatar answered Sep 20 '22 09:09

101010