如何确定C ++ 11成员函数的返回类型 [英] How can I determine the return type of a C++11 member function

查看:153
本文介绍了如何确定C ++ 11成员函数的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定各种C ++成员函数的返回类型。我知道可以使用decltype和std :: declval来做到这一点,但是我在语法上遇到了问题,并找到了有用的示例。下面的 TestCBClass 显示了一个哑类的示例,该类包含静态和普通成员函数的混合-带有&没有参数和返回类型。根据所讨论的方法,我希望能够从各个方法中声明一个返回类型的向量。

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.

在我的应用程序中,这些方法是 std :: async 的回调,我需要一个<$ c的向量$ c> std :: future<返回类型> 。我已经尝试过各种声明,例如 decltype(std :: declval(TestCBClass :: testStaticMethod))(我不确定是否需要 & )。这种语法是错误的-当然不会编译,但是我认为应该使用它的方法。

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


推荐答案

您也可以使用 std :: result_of decltype ,如果您希望列出参数类型而不是相应的伪值,例如:

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

此处演示。

这篇关于如何确定C ++ 11成员函数的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆