【发布时间】:2014-11-24 06:25:17
【问题描述】:
我正在尝试确定各种 C++ 成员函数的返回类型。我知道可以使用 decltype 和 std::declval 来执行此操作,但是我遇到了语法问题并找到了有用的示例。下面的TestCBClass 显示了一个包含混合静态和普通成员函数的哑类示例——带有& 不带参数和返回类型。根据所讨论的方法,我希望能够从每种方法中声明一个返回类型的向量。
在我的应用程序中,这些方法是std::async 的回调,我需要std::future<return types> 的向量。我尝试了各种声明,例如decltype(std::declval(TestCBClass::testStaticMethod))(我不确定在方法名称之前是否需要&)。这种语法是不正确的——当然它不能编译,但我认为它应该使用这种方法。
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;
};
【问题讨论】: