January 15, 20251 yr This article explores how C++20 concepts can replace CRTP when implementing static interfaces for a family of classes. By leveraging concepts, we achieve cleaner, more readable, and less error-prone codeÎÃöprovided you have access to C++20. Replace CRTP with Concepts by Sandor Dargo From the article: In my Meeting C++ 2024 trip report, among my favourite ideas I mentionedâ¬Ã¡Klaus IglbergerÎÃÃs talkâ¬Ã¡where he mentioned the possibility of replacing the curiously returning template pattern with the help of class tagging and concepts. Class tagging might mean different things in different contexts, or at least might be implemented in different ways. The end goal is to mark, in other words, tag classes orâ¬Ã¡functions to be used in certain contexts, with certain algorithms. As youÎÃÃll see, in our case itÎÃÃll also be a tool to prevent duck typing. We are going to see an example implementation of a static interface with CRTP with a couple of different derived classes, then weÎÃÃll see the implementation without CRTP. The CRTP solution With the static interface, we are creating a static family of types. There is no need for dynamic polymorphism to share the same interface. ItÎÃÃs still granted through a base class, which is a template taking the deriving class as a parameter. â¬Ã¡ â¬Ã¡ View the full article
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.