问题描述
我正在尝试编写一个“工厂”类模板,该模板的实例化具有可变参数的构造函数,该变量的构造函数将其参数存储在元组中,然后将这些参数传递给工厂创建的对象的构造函数。
一个简单的例子可能会更清楚:
#include <memory>
#include <tuple>
struct Foo
{
Foo(int arg1,double arg2)
{}
// ...
};
template<typename T,typename ...ARGS>
class Factory
{
public:
Factory(ARGS&&... args)
: _stored_args(std::make_tuple(std::forward<ARGS>(args)...))
{}
std::unique_ptr<T> create()
{ return std::apply(std::make_unique<T>,_stored_args); }
private:
std::tuple<ARGS...> _stored_args;
};
template<typename T,typename ...ARGS>
std::unique_ptr<Factory<T,ARGS...>> make_factory(ARGS&&... args)
{ return std::make_unique<Factory<T,ARGS...>>(std::forward<ARGS>(args)...); }
int main()
{
auto foo_factory(make_factory<Foo>(1,2.0));
auto foo_ptr(foo_factory->create());
// ...
}
我的问题是,对std::apply
的调用显然是错误的,因为gcc和clang都沿no matching function for call to '__invoke'
抱怨。我在这里做什么错了?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)