【发布时间】:2020-10-08 15:54:51
【问题描述】:
我正在尝试编写一个“工厂”类模板,其实例化具有可变参数构造函数,这些构造函数将它们的参数存储在一个元组中,然后将这些参数传递给工厂创建的对象的构造函数。
一个最小的例子可能会更清楚:
#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' 的方式抱怨。我在这里做错了什么?
【问题讨论】:
-
你能解释一下
create应该做什么吗? -
@cigien 将
unique_ptr返回到T类型的对象。我还没有解释我使用这个工厂类的目的是什么,但我认为没有必要理解这个问题。 -
不,我的意思是,是什么让你认为你需要
apply?从tuple<Args...>生成unique_ptr<T>的逻辑是什么?
标签: c++ templates c++17 variadic-templates