标准::模板参数包的 std::shared_ptr元组

std::tuple of std::shared_ptr of template parameter pack

本文关键字:shared 元组 std ptr 参数 标准      更新时间:2023-10-16

我想实现一个类模板:

  1. 行为类似于函数
  2. 它的输入和输出变量都是共享的。
  3. 相对容易使用。

因此,我构建了以下内容:

// all input/output variable's base class
class basic_logic_parameter;
// input/output variable, has theire value and iterators to functions that reference to this variable
template <typename FuncIterator, typename ValueType>
class logic_parameter
    :public basic_logic_parameter
{
private:
    std::list<FuncIterator> _refedFuncs;
    ValueType _val;
public:
};
// all `function`'s base class
class basic_logic_function
{
public:
    virtual ~basic_logic_function() = 0;
};
// the function, has input/output variable
template <typename FuncIterator, typename R, typename... Args>
class logic_function_base
    :public basic_logic_function
{
private:
    std::shared_ptr<logic_parameter<FuncIterator, R>> _ret;
    std::tuple<std::shared_ptr<logic_parameter<FuncIterator, Args>>...> _args;
public:
    template <std::size_t N>
    decltype(auto) arg()
    {
        return std::get<N>(_args);
    }
    template <std::size_t N>
    struct arg_type
    {
        typedef std::tuple_element_t<N> type;
    };
    template <std::size_t N>
    using arg_type_t = arg_type<N>::type;
    decltype(auto) ret()
    {
        return _ret;
    }
};

我希望像这样使用:

// drawing need  color and a pen
    struct Color
    {
    };
    struct Pen
    {
    };
    struct Iter
    {
    };
    class Drawer
        :public logic_function_base<Iter, void(Color, Pen)>
    {
    public:
        void draw()
        {
            arg_type_t<0> pColor; // wrong
        }
    }

我的编译器无法传递此代码,为什么?我只想将模板参数包转换为std::tuple of std::shared_ptr of them.例如:

鉴于struct A, int, struct C,我希望拥有:

std::tuple<
  std::shared_ptr<logic_parameter<A>>,
  std::shared_ptr<logic_parameter<int>>,
  std::shared_ptr<logic_parameter<C>>,
>

问题(一旦小错误被修复1)是你实例化:

logic_function_base<Iter, void(Color, Pen)>

。这意味着FuncIteratorIter的,Rvoid(Color, Pen)的,所以Args是emtpy <>,所以decltype(_args)是一个空std::tuple<>,并且你的代码无法获得空元组的第0个元素的类型,这是合法的。

你想要的是logic_function_base的部分专业化:

template <typename F, typename T>
class logic_function_base;

template <typename FuncIterator, typename R, typename... Args>
class logic_function_base<FuncIterator, R(Args...)>: public basic_logic_function {
};

1 当前代码中的小错误:

template <std::size_t N>
struct arg_type
{
    typedef std::tuple_element_t<N, decltype(_args)> type; // Missing the tuple type
};
template <std::size_t N>
using arg_type_t = typename arg_type<N>::type; // Missing a typename

这可能无法回答您的整个问题,但您可以使用以下特征来包装元组元素类型。

template <typename T> struct wrap;
template <typename... T>
struct wrap<std::tuple<T...>> {
  using type = std::tuple<std::shared_ptr<logic_parameter<T>>...>;
}
template <typename T>
using wrap_t = typename wrap<T>::type;

然后,您可以像这样使用它:

std::tuple<int,double,char> t1;
wrap_t<decltype(t)> t2;

t2的类型是 std::tuple<std::shared_ptr<logic_parameter<int>>,std::shared_ptr<logic_parameter<double>>,std::shared_ptr<logic_parameter<char>>> .