Inspired by a similar previous thread showcasing cool uses for C++26 reflection.
Power of C++26 Reflection: Strong (opaque) type definitions
From the article:
With reflection, you can easily create "opaque" type definitions, i.e "strong types". It works by having an inner value stored, and wrapping over all public member functions.
Note: I am using queue_injection { ... } with the EDG experimental reflection, which afaik wasn't actually integrated into the C++26 standard, but without it you would simply need two build stages for codegen. This is also just a proof of concept, some features aren't fully developed (e.g aggregate initialization)
struct Item { /* ... */ }; // name, price as methods struct FoodItem; struct BookItem; struct MovieItem; consteval { make_strong_typedef(^^FoodItem, ^^Item); make_strong_typedef(^^BookItem, ^^Item); make_strong_typedef(^^MovieItem, ^^Item); } // Fully distinct types void display(FoodItem &i) { std::cout << "Food: " << i.name() << ", " << i.price() << std::endl; } void display(BookItem &i) { std::cout << "Book: " << i.name() << ", " << i.price() << std::endl; } int main() { FoodItem fi("apple", 10); // works if Item constructor isn't marked explicit FoodItem fi_conversion(Item{"chocolate", 5}); // otherwise BookItem bi("the art of war", 20); MovieItem mi("interstellar", 25); display(fi); display(bi); // display(Item{"hello", 1}); // incorrect, missing display(Item&) function // display(mi); // incorrect, missing display(MovieItem&) function }

Add a Comment
Comments are closed.