3
#include <tuple>
class Foo {
public:
Foo(int i, double d, const char* str) { }
};
template<class T, class... CtorArgTypes>
class ObjectMaker {
public:
ObjectMaker(CtorArgTypes... ctorArgs) : m_ctorArgs(ctorArgs...)
{
}
Foo* create()
{
//What do I do here?
}
private:
std::tuple<CtorArgTypes...> m_ctorArgs;
};
int main(int, char**)
{
ObjectMaker<Foo, int, double, const char*> fooMaker(42, 5.3, "Hello");
Foo* myFoo = fooMaker.create(); //this should do new Foo(42, 5.3, "Hello");
}
基本上,我希望類ObjectMaker
保存傳遞給構造函數Foo
的參數,並在調用ObjectMaker::create()
時使用它們。我無法弄清楚的是如何從tuple
中獲取值到Foo
的構造函數?如何使用std :: tuple中的值作爲函數參數?
你可以看看[這個問題](http://stackoverflow.com/a/4121942/246886)。 –
[「解包」一個元組以調用匹配函數指針]的可能重複(http://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer) – Xeo
Btw ,請不要使用HTML標記進行代碼標記,對於內嵌塊和四個前導空格(或標記和ctrl-K)使用反引號代碼塊。 – Xeo