如何在可变的类模板中获得类型的索引?

How can I get the index of a type in a variadic class template?

本文关键字:类型 索引      更新时间:2023-10-16

我有一个可变引擎模板类:

template <typename ... Components> class Engine;

我想在编译时为每个组件分配一个数字,这相当于它们的顺序。这将在执行以下调用时返回:

template <typename Component> int ordinal();

例如:

Engine<PositionComponent, PhysicsComponent, InputComponent> engine;

被声明,调用:

engine.ordinal<PhysicsComponent>();

将返回1,类似的调用InputComponent而不是PhysicsComponent将返回2。

这是可能的吗?如果是,一个人会怎么做?

Mp11,这是一个简短的一行代码(一如既往):

template <typename... Components>
struct Engine {
    template <typename Component>
    static constexpr int ordinal() {
        return mp_find<Engine, Component>::value;
    }
};

注意,如果Component不存在,这将返回sizeof...(Components)。如果需要,您可以添加一个静态断言来验证这一点。

我的原始答案如下…


那么你想求ComponentComponents...中的指数?

template <typename... >
struct index;
// found it
template <typename T, typename... R>
struct index<T, T, R...>
: std::integral_constant<size_t, 0>
{ };
// still looking
template <typename T, typename F, typename... R>
struct index<T, F, R...>
: std::integral_constant<size_t, 1 + index<T,R...>::value>
{ };

用法:

template <typename Component> 
size_t ordinal() { return index<Component, Components...>::value; }

作为构造,试图获得Component不在Components...中的ordinal将导致编译错误。

我下面的目标是尽可能地保持在编译时领域。

这是删除一些样板文件的别名。std::integral_constant是一个很棒的std类型,它存储编译时确定的整型:

template<std::size_t I>
using size=std::integral_constant<std::size_t, I>;

下一步,index_of类型,和index_of_t稍微容易使用:

template<class...>struct types{using type=types;};
template<class T, class Types>struct index_of{};
template<class T, class...Ts>
struct index_of<T, types<T, Ts...>>:size<0>{};
template<class T, class T0, class...Ts>
struct index_of<T, types<T0, Ts...>>:size<
  index_of<T,types<Ts...>>::value +1
>{};

这个别名返回一个纯std::integral_constant,而不是从它继承的类型:

template<class T, class...Ts>
using index_of_t = size< index_of<T, types<Ts...>>::value >;

最后我们的函数:

template <class Component>
static constexpr index_of_t<Component, Components...>
ordinal() const {return{};}

不仅是constexpr,它返回一个将其值编码为其类型的值。size<?>有一个constexpr operator size_t()和一个operator(),所以你可以在大多数需要无缝整型的地方使用它。

你也可以使用:

template<class Component>
using ordinal = index_of_t<Component, Components...>;

和现在的ordinal<Component>是一个表示组件索引的类型,而不是一个函数

为了完整起见,我添加了这个,它利用了c++ 11的constexpr功能和一些stl函数。我觉得它比其他解决方案更干净一点。

//Same type
template <typename Target,typename T,typename ...Rest>
constexpr typename std::enable_if<std::is_same<Target,T>::value, size_t>
_ordinal(){
    return 0;
}
//Different types
template <typename Target,typename T,typename ...Rest>
constexpr typename std::enable_if<!std::is_same<Target,T>::value, size_t>
_ordinal(){
    return 1+_ordinal<Target,Rest...>();
}

未测试:

template <int, typename>
constexpr int index_of() { return -1; } // type not found
template <int N, typename Component, typename Cur, typename... Components>
constexpr int index_of() {
    return std::is_same<Component, Cur>::value ? N : index_of<N+1, Component, Components...>();
}
template <typename... Components>
template <typename Component>
constexpr int engine<Components...>::ordinal() {
    return index_of<0, Component, Components...>();
}

可以使用结构体,但是我发现这样更简洁(没有::type的丑陋)。

如果您希望在未找到类型时出现编译时错误,请将ordinal更改为:

template <typename... Components>
template <typename Component>
constexpr int engine<Components...>::ordinal() {
    static_assert(index_of<0, Component, Components...>()!=-1, "invalid component");
     return index_of<0, Component, Components...>();
}