如何根据数组元素自动创建模板 类 的元组?

How to automate creation of tuples of template classes based on elements of an array?

我有一个 std::array,其中包含所有类型的枚举。我想实现我的元组 关于这个。

class CompBase
{
public:
    enum CompType{
        INPUT,
        GRAPHICS
        // ( + 5-10 additional types)
    };

    static const std::array<CompType, 2> compTypeArr;
};

const std::array<CompBase::CompType, 2> CompBase::compTypeArr =
{
    CompBase::INPUT,
    CompBase::GRAPHICS
};

template<CompBase::CompType compType_e>
class CompHolder {}; // owns one component

template<CompBase::CompType compType_e>
class CompContainer {}; // references N components

class CompInterface
{
    // ...
private:
    std::tuple // I want to automate this,
    <
        CompHolder<CompBase::INPUT>,
        CompHolder<CompBase::GRAPHICS>
    > compHolders;
};

class CompHandler
{
    // ...
private:
    std::tuple // and this process, based on the predefined array
    <
        CompCont<CompBase::INPUT>,
        CompCont<CompBase::GRAPHICS>
    > compContainers;
};

据我了解 std::make_tuple 甚至 constexpr 之前 c++14 ref 所以我不确定这是否可行,因为我需要一个 c++11 方法。我认为数组的存在是强制性的,因为单独的枚举并不能为这样的事情提供必要的功能。

这是一个适用于 C++11 的解决方案。正如评论中所讨论的那样,使用 std::array 可以在 C++14 中使用,它的 const 访问器是 constexpr.

#include <tuple>
#include <type_traits>
#include <iostream>

class CompBase
{
public:
   enum CompType {
      INPUT,
      GRAPHICS
      // ( + 5-10 additional types)
   };
};

template<CompBase::CompType...> struct CTLHelper;
using CompTypeList = CTLHelper<
   CompBase::INPUT,
   CompBase::GRAPHICS
>;

template<template<CompBase::CompType> class, class> struct CompTupleMaker;
template<template<CompBase::CompType> class H, CompBase::CompType... Es> 
struct CompTupleMaker<H, CTLHelper<Es...>>
{
   using type = std::tuple<H<Es>...>;
};

template<CompBase::CompType compType_e>
class CompHolder {}; // owns one component

template<CompBase::CompType compType_e>
class CompContainer {}; // references N components

using CompHolderTuple = CompTupleMaker<CompHolder, CompTypeList>::type;
using CompContainerTuple = CompTupleMaker<CompContainer, CompTypeList>::type;

class CompInterface
{
   // ...
private:
   CompHolderTuple compHolders;
};

class CompHandler
{
   // ...
private:
   CompContainerTuple compContainers;
};


int main()
{
   // just a quick check
   std::cout << std::is_same<CompHolderTuple, std::tuple<CompHolder<CompBase::INPUT>, CompHolder<CompBase::GRAPHICS>>>::value << '\n';
}

如果您确实需要 C++11 中的数组中的这些枚举器,您可以使用声明为 constexpr 的内置数组 - 您可以在常量表达式中引用其元素。我想说的是,只有当您还需要数组来做其他事情时才有意义;如果您只需要一个枚举器列表的持有者,那么可变参数解决方案在这种情况下会更简单。对于数组解决方案,您需要类似于 C++14 的 std::index_sequence 来扩展数组。