TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24812 16
Power of C++26 Reflection: Strong (opaque) type definitions

Inspired by a similar [previous thread](https://www.reddit.com/r/cpp/comments/1mwv927/the_power_of_c26_reflection_first_class/) showcasing cool uses for C++26 reflection.

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)

[godbolt](https://godbolt.org/z/8n4r5Ydvz)

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
}

https://redd.it/1rgo4s9
@r_cpp
Reddit From the cpp community on Reddit: The power of C++26 reflection: first class existentials Posted by geekfolk - 100 votes and 99 comments
More from @r_cpp
  1. Oct 7, 2026New C++ Conference Videos Released This Month - September 2026 C++Now 2026-08-31 - 2026-09…
  2. Oct 7, 2026Jean Frantz René: C++ Memory safety for safety-critical and non-safety critical applicatio…
  3. Oct 7, 2026Full Meeting C++ 2026 program released https://meetingcpp.com/mcpp/schedule/#2026 https://…
  4. Oct 7, 2026Qt moc now can handle basic C++ modules (Qt 6.12) https://codereview.qt-project.org/c/qt/q…
  5. Oct 7, 2026New to c++ Hello everybody,im new to cpp,and want to learn it,i have some knowledge with C…
  6. Oct 7, 2026Optimizing the Compressed Log Pipeline: https://github.com/Tencent/BqLog/blob/main/docs/Ar…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →