0

In the project I'm working on we have a large number of classes with methods named to match enum keys. This provides a means to call functions that can be dynamically detected.

General example of these classes:

template <class ImplType>
class BaseTemplate
{
...
};

class A : public BaseTemplate<A>
{
public:
  enum class Methods : int
  {
     DoAThing,
     SecondItem,
     ItemNumberThree,
     FinalAction
  };

private: 
  void doAThing();
  void secondItem();
  void itemNumberThree();
  void finalAction();
};

The methods are called by another mechanism (more on this below). I'd like a means to have a compile-time check that the method names match the keys in the enum, so to have a compiler error if a key in the enum is not matched by a function.

Notes:

  • The classes inherit a base class with other business functionality (not shown). Since this is using CRTP to capture the type of the derived object, we would ideally add checks there.
  • I can use C++20 and potentially C++23, so consteval and similar features are available.
  • As in the example, enum keys must be CapitalCamelCase, methods named lowerCamelCase. The only difference is the case of the first letter.
  • This is a Qt project -- the enum here is a Q_ENUM and the methods are private Q_SLOT. I've not put these in the example, as if possible I'd prefer a pure C++ solution.
    In the actual application, we use meta-object reflection to enumerate the enum keys, and so to achieve calls to the possible methods. However, I suspect the Qt metaobject system is unlikely to be useful for compile-time checks. We already have run-time assertions on missing methods. If Qt can somehow be used for this, perhaps after the Moc compile step, that would be acceptable.
10
  • 2
    C++ doesn't have any reflection, so getting names of things is not possible. You could probably conjure up a macro that declares both things at the same time, thus avoiding the problem of checking if they match, use compiler specific API to get to AST internals (clang has something like that, not sure about gcc) or write external tool that you run in your CI or as post-commit hook or whatever. Commented May 13, 2024 at 10:05
  • 2
    You can write however weird coding constraints checking and/or repairing tools. Text in text out. You can write such tools in pure C++, no usage of Qt or other extensions is needed. Add usage of those tools to your compiling and code processing similarly like that QT moc is added to it. Commented May 13, 2024 at 10:22
  • @Yksisarvinen yes - might have to wait a few years for reflection. Yes, a macro might work but as I don't think an enum can be re-opened, I'd probably have to do the enum as-is. I might be able to declare the methods with a macro that constevals the matching Enum key, but this doesn't check that all Enum keys have a method. Commented May 13, 2024 at 11:22
  • @Yksisarvinen as a side note, we have "magic_enum" which does a reasonably decent job of getting names for enum values -- I love using that for things like debug outputs, and sometimes even user-facing outputs. However, I definitely don't see a way to use such names as method names... not even at runtime. Commented May 13, 2024 at 11:31
  • I would use the enum values as NTTP to a generic function that handles everything. Ideally, the template shouldn't be a none-static member(this would be the 1st argument). And for template functions, I'd rather use a lambda or CPO - such that std::bind and other functional manipulations be easier. Commented May 13, 2024 at 14:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.