Skip to content

Add support for functors which are constexpr instances instead of types

Gerhard Raven requested to merge functors-allow-instances into 2024-patches

Currently the python grammar always maps Functors onto types, which implies that if one has a collection of functors which is almost, but not quite, the same, one must eg. template the functor type in order to differentiate amongst them. This MR allows one to specify that a Functor is an instance (which addresses this comment) instead, which enables the creation of multiple constexpr instances of the same type, but where each instance is constructed slightly differently, and named appropriately. This reduces the reliance on the type system, and instead pushes information from the type system to (constant) data members, and as keeping track of variables (which have finite lifetimes) is, from first principles, easier for a compiler than keeping track of types (which, once created, must be known for the duration of the compilation step), it should help reduce the resources needed to compile the functor code, and produce smaller libraries.

Perhaps an example will help explain the difference. Previously, there was:

  using LHCEnergy = details::DeLHCInfoFunctor<&LHCb::Detector::LHCInfo::lhcenergy>;
  using LHCbClockPhase = details::DeLHCInfoFunctor<&LHCb::Detector::LHCInfo::lhcbclockphase>;

which were almost identical functors to access LHCInfo -- the only difference is in which member function of LHCInfo to use, and this member function was specified as a template parameter. This can now be written as:

  inline constexpr auto LHCEnergy = details::DeLHCInfoFunctor{&LHCb::Detector::LHCInfo::lhcenergy};
  inline constexpr auto LHCbClockPhase = details::DeLHCInfoFunctor{&LHCb::Detector::LHCInfo::lhcbclockphase};

instead, i.e. now there is a single, non-templated type DeLHCInfoFunctor which accepts the member function to use as a constructor argument, and stores (the pointer to) that function as member data.

Edited by Gerhard Raven

Merge request reports