c++ - 从参数包创建索引和类型对

标签 c++ c++14 variadic-templates

我正在尝试创建一个元组或一对索引参数包。

以下是代码想要实现的示例:

  {
    using Indices =
        std::tuple< IndexTypePair< 0, int >, IndexTypePair< 1, int >, IndexTypePair< 2, float > >;
    test2< Indices, 0 >();
    test2< Indices, 1 >();
    test2< Indices, 2 >();
  }

这是概括:

  {
    template < typename... T >
    using make_index_tuple = tuple< IndexTypePair< Index_v< T, T... >, T >... >;
    using Indices = make_index_tuple< int, int, float >;
    test2< Indices, 0 >();
    test2< Indices, 1 >();
    test2< Indices, 2 >();
  }

此解决方案基于此问题的答案: How to get the index of a type in a variadic type pack?

我遇到的问题之一是创建可变参数模板别名,前面的代码给出以下错误:

$ make index-sequence clang++-3.8 -Wall -Werror -Wextra -std=c++14
-pedantic -Wconversion -stdlib=libc++ -O3 -pthread    index-sequence.cpp   -o index-sequence index-sequence.cpp:50:5: error: expected expression
    template < typename... T >
    ^ index-sequence.cpp:52:21: error: unknown type name 'make_index_tuple'
    using Indices = make_index_tuple< int, int, float >;
                    ^ ...

以下是使该代码正常工作的其他一些细节:

#include <array>
#include <cstddef>
#include <iostream>
#include <type_traits>
#include <utility>

template < typename T, typename... Ts >
struct Index;

template < typename T, typename... Ts >
struct Index< T, T, Ts... > : std::integral_constant< std::size_t, 0 >
{
};

template < typename T, typename U, typename... Ts >
struct Index< T, U, Ts... > : std::integral_constant< std::size_t, 1 + Index< T, Ts... >::value >
{
};

template < typename T, typename... Ts >
constexpr std::size_t Index_v = Index< T, Ts... >::value;

template < size_t i, typename T >
struct IndexTypePair
{
  static constexpr size_t index{i};
  using Type = T;
};

template < typename Idx, size_t i >
void test2()
{
  using ITP = typename std::tuple_element< i, Idx >::type;
  typename ITP::Type v{};
  //....
  std::cout << ITP::index << ' ' << v << std::endl;
}

最佳答案

使用嵌套结构设置以及标准 make_index_sequence 来帮助您:

template < typename ... T >
struct make_index_type_tuple_helper
{    
    template< typename V >
    struct idx;

    template< size_t ... Indices >
    struct idx<std::index_sequence<Indices...>>
    {
        using tuple_type = std::tuple<IndexTypePair<Indices, T>...>;
    };

    using tuple_type = typename idx<std::make_index_sequence<sizeof...(T)>>::tuple_type;
};

template < typename ... T>
using make_index_type_tuple = typename make_index_type_tuple_helper<T...>::tuple_type;

然后您就可以按照您想要的方式使用它( live demo ):

using Indices = make_index_type_tuple< int, int, float >;
test2< Indices, 0 >();
test2< Indices, 1 >();
test2< Indices, 2 >();

关于c++ - 从参数包创建索引和类型对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746040/

相关文章:

c++ - 重载时区分函数

c++ - 如何处理模板类中的指针?

c++ - 是否可以像在其他编译器中的 Microsoft C 中那样使用类型化名称或类型名称来声明构造函数?

c++ - 没有 constexpr 的模板非类型参数的类型转换

c++ - 为什么调用转发引用构造函数而不是复制构造函数?

c++ - 可以接受未知签名的可变函数引用的类

c++ - 如何在 VS C++ 上初始化 boost::edge_weight_t 类型

c++ - 右值保证了什么样的优化?

c++ - 覆盖具有不同返回类型的基类函数

c++ - 参数列表中间的可变模板参数