我以前這裏的問題Variadic template, function as argument調用帶有一個成員函數可變參數模板函數基於
我已經結束了下面一個簡單的類,但我怎麼使用std ::調用或一些其他方法來調用測試: :用Tester :: simple作爲參數測試? http://en.cppreference.com/w/cpp/utility/functional/invoke#Example提供的例子對我來說不是很有幫助,我嘗試了很多不同的使用std :: invoke的方法。
class Tester {
public:
template<typename F, typename... Args>
decltype(auto) test(F&& f, Args&&... args) {
return std::forward<F>(f)(std::forward<Args>(args)...);
}
int simple(int i) { return i; }
};
int main()
{
Tester t;
std::cout << t.test(t.simple, 3); // how do you modify this line such that the code compiles?
}
謝謝!
現在它與非成員函數不兼容。試試't.test(放,「你好!」)'。 –
@HenriMenke確實如此。據我所知,OP在這個問題上有興趣處理成員函數。所以我爲成員函數添加了一個簡單的解決方案。現在,OP可以自由結合他的問題解決方案來實現所需的功能。或者,如果他的編譯器允許他這樣做,只需使用'std :: invoke'即可:) –