【问题标题】:std::tuple of std::shared_ptr of template parameter pack模板参数包的 std::shared_ptr 的 std::tuple
【发布时间】:2017-01-11 15:46:00
【问题描述】:

我想实现一个类模板:

  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>>,
>

【问题讨论】:

  • 您的代码中有明显错误(std::tuple_element_t 缺少tupletypename 缺少),这些是复制/粘贴错误吗?
  • R(Args...) 本身并没有神奇地扩展为 typename R, typename... Args,perpahs use a partial specialization and fix a couple of errors
  • @Holt 这是一个打字错误:)
  • @PiotrSkotnicki 我稍后会尝试。谢谢!

标签: c++ c++11 templates


【解决方案1】:

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

logic_function_base<Iter, void(Color, Pen)>

...意思是FuncIteratorIterRvoid(Color, Pen),所以Args是emtpy&lt;&gt;,所以decltype(_args)是一个空的std::tuple&lt;&gt;,你的代码不能获取空元组第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

【讨论】:

    【解决方案2】:

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

    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&lt;std::shared_ptr&lt;logic_parameter&lt;int&gt;&gt;,std::shared_ptr&lt;logic_parameter&lt;double&gt;&gt;,std::shared_ptr&lt;logic_parameter&lt;char&gt;&gt;&gt;

    【讨论】:

    • 感谢您的回复
    • @ChungkingExpress 在 stackoverflow 上习惯于投票,而不是在 cmets 中感谢。但不客气。干杯。
    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2023-03-11
    • 2021-04-09
    相关资源
    最近更新 更多