简体   繁体   中英

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 . I'm using Boost Hana and have a function which should look like this:

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. Signature is a hana::tuple_t of ECS component and tag types. primitiveBit returns bit offset of template argument type component/tag. As hana::fold lambda doesn't provide type of current r , I can't simply call primitiveBit<RType> (RType is not defined).

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

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.

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.

Using template <template<typename T> typename TReturn, typename T> isn't an option due to T not being defined within lambdas.

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.

Which means propertyCheck<typename decltype(r)::type> works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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