我試圖確定各種C++成員函數的返回類型。我明白,decltype和std :: declval可以用來做到這一點,但我遇到了語法問題並找到了有用的例子。下面的TestCBClass
顯示了一個包含混合靜態和常規成員函數的啞類的示例 - 帶有無參數和返回類型的&。根據所討論的方法,我希望能夠從各種方法中聲明一個返回類型的向量。如何確定C++ 11成員函數的返回類型
在我的應用程序中,這些方法是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;
};
非常好,簡單!謝謝 – johnco3 2014-09-29 20:31:18