c++ - 解包方法参数的元组

标签 c++ tuples c++14

我在将元组扩展为其内容时遇到语法问题。

我的工作代码:

class Example
{
    public:
        static void Go( int i, float f)
        {
            std::cout << "p1: " << i << std::endl;
            std::cout << "p2: " << f << std::endl;
        }

        template <typename T, size_t ... I>
            static void Do( T parm )
        {
            Go( std::get<I>( parm)...);
        }
};

int main()
{
    using X = std::tuple<int, float>;
    Example::Do<X,0,1>( std::make_tuple( 1,2.2)) ;
}

但是我想用类似的东西来调用扩展

int main()
{
    using X = std::tuple<int, float>;
    using IDX = std::std::index_sequence_for<int, float>;
    Example::Do<X,IDX>( std::make_tuple( 1,2.2)) ;
}

所以我正在寻找类似的东西(无法编译......):

template <typename T, size_t ... I>
static void Do<T, std::index_sequence<I...>>(T parm)
   {
            Go( std::get<I>( parm)...);
   }

最佳答案

按值传递索引序列:

template <typename T, size_t... I>
static void Do(T parm, std::index_sequence<I...>)
{
    Go(std::get<I>(parm)...);
}

像这样调用方法:

Example::Do(std::make_tuple(1, 2.2), 
    std::index_sequence_for<int, float>{});

关于c++ - 解包方法参数的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37027095/

相关文章:

python - 根据输入,在列表上使用 tuple() 不会返回相同的元组

c++ - QDialog 返回值,仅接受或拒绝?

c++ - 带有声明的析构函数的自动生成的默认移动构造函数

c++函数将time_t格式化为std::string:缓冲区长度?

c++ - 我可以在 STL 中禁用异常吗?

python - 转换为 numpy 数组的元组列表

c++ - 我是否正确地排除了 base 的 fn() 函数?

c++ - LPWSTR 到 std::string?

c++ - STL::map 插入一个带约束的值

python - 如何使用 matplotlib 绘制元组列表?