Created
May 22, 2026 20:00
-
-
Save unrays/4eae18cf6a6e46e83c071fdaa1d03205 to your computer and use it in GitHub Desktop.
Exotic CRTP pattern for static polymorphism using variadic mixin composition and C++23 explicit object parameters. Introduces a novel approach to compile-time interface dispatch without runtime overhead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (c) May 2026 Félix-Olivier Dumas. All rights reserved. | |
| // Licensed under the terms described in the LICENSE file | |
| // Reference example of the pattern | |
| // See: https://medium.com/@felixolivierdumas/exotic-crtp-rethinking-static-polymorphism-with-c-23-89f9e75e8ffd | |
| #pragma once | |
| #include <iostream> | |
| #include <type_traits> | |
| #include <utility> | |
| namespace exotic { | |
| template<typename... From> | |
| struct crtp_access : From... {}; | |
| template<typename T> | |
| constexpr decltype(auto) as_crtp(T&& obj) noexcept { | |
| using crtp_access_t = crtp_access<std::remove_cvref_t<T>>; | |
| return static_cast<crtp_access_t&&>(obj); | |
| } | |
| } | |
| struct Base { | |
| void interface(this auto&& self) { | |
| return exotic::as_crtp(self).implementation(); | |
| } | |
| }; | |
| struct Derived : Base { | |
| void implementation(this exotic::crtp_access<Derived> self) { | |
| std::cout << "Derived implementation" << std::endl; | |
| } | |
| }; | |
| int main() { | |
| Derived d; | |
| d.interface(); // perfectly works | |
| //d.implementation(); -> doesn't work, Derived only shows .interface() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment