简体   繁体   English

将模板化的constexpr传递给函数来推断自动对象的类型

[英]Passing templated constexpr to function inferring type of auto object

I'm making an entity-component system library utilizing template metaprogramming to evaluate signature bitset data at compile-time and allow for precise bitset size without using #define MAX_COMPONENTS some-number . 我正在使用模板元编程制作一个实体组件系统库,以在编译时评估签名位集数据,并允许精确的位集大小,而无需使用#define MAX_COMPONENTS some-number I'm using Boost Hana and have a function which should look like this: 我正在使用Boost Hana,并具有应如下所示的功能:

template <auto T>
  static constexpr Bitset signatureBitset = Bitset(
    hana::fold(SignatureList[hana::integral_c<std::size_t, signatureID<T>>], 0,
    [](auto l, auto r) {
      return l | 1 << inferProperty<primitiveBit>(r);
    }));

What it does is calculate constexpr Bitset for given signature. 它的作用是为给定签名计算constexpr位集。 Signature is a hana::tuple_t of ECS component and tag types. 签名是ECS组件和标签类型的hana :: tuple_t primitiveBit returns bit offset of template argument type component/tag. primitiveBit返回模板参数类型组件/标签的位偏移量。 As hana::fold lambda doesn't provide type of current r , I can't simply call primitiveBit<RType> (RType is not defined). 由于hana :: fold lambda不提供当前r类型,因此我不能简单地调用primitiveBit<RType> (未定义RType)。

Simplest possible solution would be to write a duplicate of every template "function" but as an actual constexpr function instead of static constexpr object but I'm trying to avoid that as writing 30+ duplicate function which all do 最简单的解决方案是编写每个模板“函数”的副本,但将其作为实际的constexpr函数而不是静态constexpr对象,但是我试图避免这样做,因为编写30多个重复函数都可以

template <typename T>
static constexpr auto inferFunctionName(hana::basic_type<T> t) {
    return functionName<T>;
}

seems dumb and will make everything harder to maintain. 似乎很愚蠢,会使所有内容难以维护。 The above code also looks very simple and like it can be abstracted away using template function taking template constexpr object as a template parameter. 上面的代码看起来也非常简单,并且可以使用模板函数(将模板constexpr对象作为模板参数)抽象出来。

This is what I currently have: 这是我目前拥有的:

template <template<typename> typename TReturn, typename T>
constexpr auto inferProperty(hana::basic_type<T> t) {
    return TReturn<T>();
}

inferProperty<primitiveBit>(r) throws a compile error saying it doesn't match defined template signature. inferProperty<primitiveBit>(r)抛出编译错误,指出它与定义的模板签名不匹配。

Using template <template<typename T> typename TReturn, typename T> isn't an option due to T not being defined within lambdas. 使用template <template<typename T> typename TReturn, typename T>由于在lambda中未定义T不能选择template <template<typename T> typename TReturn, typename T>

Simple solution, as said by Jack C. in the comments, is to use decltype(r) inside lambda to get type from object instead of inferring type through template functions. 正如Jack C.在评论中所说,一种简单的解决方案是在lambda中使用decltype(r)从对象获取类型,而不是通过模板函数来推断类型。

Which means propertyCheck<typename decltype(r)::type> works. 这意味着propertyCheck<typename decltype(r)::type>有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM