今日已更新 281 条资讯 | 累计 21147 条内容
关于我们

Exotic CRTP: Enforcing Strict Interfaces Without Friends Using C++23 Explicit Object Parameters

/u/Mysticatly 2026年06月02日 23:59 3 次阅读 来源:Reddit r/programming

I’ve been experimenting with CRTP and ended up with a variation that enforces a strict interface/implementation boundary without friend declarations. The goal was to eliminate boilerplate I frequently encountered when trying to encapsulate derived class methods. The key idea is using C++23 explicit object parameters this + a small access wrapper type so implementations can only be called through the interface layer. That was about two and a half months ago. Since, I’ve taken the time to better understand it and write an article about it, which you can find below. As explained there, I refer to this approach as Exotic CRTP. Example ```cpp // Reference example of the pattern // See: https://medium.com/@felixolivierdumas/exotic-crtp-rethinking-static-polymorphism-with-c-23-89f9e75e8ffd 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) { 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 allows .interface() } ``` Not sure yet if this is actually useful in real conditions or just a different way of structuring CRTP, but it seems to be genuinely powerful. Full write-up here: https://medium.com/@felixolivierdumas/exotic-crtp-rethinking-static-polymorphism-with-c-23-89f9e75e8ffd Curious how this compares to traditional CRTP + friend patterns in real codebases :) submitted by /u/Mysticatly [link] [留言]

本文内容来源于互联网,版权归原作者所有
查看原文