0

I am trying to create and then check the list of types registered at compile time and then create and serve instances of those types from templated functions. As an example:

class Widgets
{
    template<typename T>
    void addWidget()
    {
         // register T in a type list at compile time here
    }

    template<typename T>
    T& getWidget()
    {
         // compile time check to see if T is registered
         // if registered then return a reference from a runtime map
     }

     void init()
     {
         // create a runtime type->instance map from registered types
     }
}

// some where else in the code in a different class
SizeWidget& w = widgets.getWidget<SizeWidget>(); // this generates compiler error if SizeWidget is not registered

boost fusion looks like a good candidate for this except adding to the fusion map is immutable and returns a new type. This solution C++ type registration at compile time trick works but not from templated functions

Is this even possible?

  • The only thing I'd interpret as "registering at compile time" would be determining a `constinit` object based on the registered types, but a constinit object must be directly initialized and cannot be modified by any means at compile time afterwards. At this point simply using regular template metaprogramming would be far simpler to implement than anything you seem to be attempting here... – fabian Aug 03 '22 at 19:19
  • @fabian Thank you for the comment. Can you point me to an example of how this can be achieved with regular TMP? – signedbit Aug 03 '22 at 19:24

0 Answers0