首页 > 解决方案 > 在工厂类中存储和传递参数包

问题描述

我正在尝试编写一个“工厂”类模板,其实例化具有可变参数构造函数,这些构造函数将它们的参数存储在一个元组中,然后将这些参数传递给工厂创建的对象的构造函数。

一个最小的例子可能会更清楚:

#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'. 我在这里做错了什么?

标签: c++templatesc++17variadic-templates

解决方案


您需要做的就是将std::make_unique调用包装成一个完美转发的 lambda:

std::unique_ptr<T> create() {
return std::apply(
    [](auto&&... xs) {
        return std::make_unique<T>(std::forward<decltype(xs)>(xs)...);
    },
    _stored_args);
}

godbolt.org 上的实时示例

原因是它std::make_unique不仅需要一个T模板参数,而且还需要Args...,在这种情况下,它是由 fowarding 推导出来的xs...。请参阅cppreference


推荐阅读