Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    U Mobile deploys ULTRA5G in Kota Kinabalu

    AKASO Launches Keychain 2: A Pocket-Sized 4K Action Camera Built for Creators on the Move

    Huawei Malaysia beings preorders for Pura 80

    Facebook X (Twitter) Instagram
    • Artificial Intelligence
    • Business Technology
    • Cryptocurrency
    • Gadgets
    • Gaming
    • Health
    • Software and Apps
    • Technology
    Facebook X (Twitter) Instagram Pinterest Vimeo
    Tech AI Verse
    • Home
    • Artificial Intelligence

      Blue-collar jobs are gaining popularity as AI threatens office work

      August 17, 2025

      Man who asked ChatGPT about cutting out salt from his diet was hospitalized with hallucinations

      August 15, 2025

      What happens when chatbots shape your reality? Concerns are growing online

      August 14, 2025

      Scientists want to prevent AI from going rogue by teaching it to be bad first

      August 8, 2025

      AI models may be accidentally (and secretly) learning each other’s bad behaviors

      July 30, 2025
    • Business

      Why Certified VMware Pros Are Driving the Future of IT

      August 24, 2025

      Murky Panda hackers exploit cloud trust to hack downstream customers

      August 23, 2025

      The rise of sovereign clouds: no data portability, no party

      August 20, 2025

      Israel is reportedly storing millions of Palestinian phone calls on Microsoft servers

      August 6, 2025

      AI site Perplexity uses “stealth tactics” to flout no-crawl edicts, Cloudflare says

      August 5, 2025
    • Crypto

      Japan Auto Parts Maker Invests US Stablecoin Firm and Its Stock Soars

      August 29, 2025

      Stablecoin Card Firm Rain Raise $58M from Samsung and Sapphire

      August 29, 2025

      Shark Tank Star Kevin O’Leary Expands to Bitcoin ETF

      August 29, 2025

      BitMine Stock Moves Opposite to Ethereum — What Are Analysts Saying?

      August 29, 2025

      Argentina’s Opposition Parties Reactivate LIBRA Investigation Into President Milei

      August 29, 2025
    • Technology

      It’s time we blow up PC benchmarking

      August 29, 2025

      If my Wi-Fi’s not working, here’s how I find answers

      August 29, 2025

      Asus ROG NUC 2025 review: Mini PC in size, massive in performance

      August 29, 2025

      20 free ‘hidden gem’ apps I install on every Windows PC

      August 29, 2025

      Lowest price ever: Microsoft Office at $25 over Labor Day weekend

      August 29, 2025
    • Others
      • Gadgets
      • Gaming
      • Health
      • Software and Apps
    Check BMI
    Tech AI Verse
    You are at:Home»Technology»C++26 Reflections adventures and compile-time UML
    Technology

    C++26 Reflections adventures and compile-time UML

    TechAiVerseBy TechAiVerseAugust 3, 2025No Comments2 Mins Read3 Views
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    C++26 Reflections adventures and compile-time UML
    Share
    Facebook Twitter LinkedIn Pinterest WhatsApp Email

    BMI Calculator – Check your Body Mass Index for free!

    C++26 Reflections adventures and compile-time UML

    The first thing I do every time I need to learn a new codebase is to start drawing the UML diagram of its classes, and usually give up soon after I started. The process of doing it manually is certainly useful, but now with reflections I figure it would be fun to try generate it instead.

    With C++26 reflections[1] the general consensus is that the magnitude of language change is comparable to what happened with C++11. After my little experiment with it, I would cautiously agree. So how does one go about creating a (Plant)UML diagram at compile time? With something like this.

    With the spoilers out of the way let’s dig in the details.

    P2996[1] introduces a couple of operators, namely lift ^^ and splice [: :]. The first one “lifts” a type or variable into a “meta” space, and the “splice” one (which imho should have been called “grounding operator”) does the opposite.

    The first thing to understand is that regardless of what we apply the lift operator (^^) to, it creates a std::meta::info type. This means that some info objects will be reflections of types, and some will be reflections of values. This creates confusion in my head, as at times one needs to check which kind of info something is, but there are good reasons for this and are well explained in section 2.2 of the paper. With that in mind let’s start coding.

    int main() {
      MyClass s;
      constexpr const char* const dot_graph_uml = make_class_graph();
    
      std::cout << dot_graph_uml << std::endl;
    }

    You’ll notice right away there is an ugly, and seemingly avoidable char pointer. Let’s get back to that later. Not much happens in main() besides us calling function template that is just a wrapper for what we actually care about:

    template
    consteval const char* make_class_graph() { 
      std::string graph = "@startuml nskinparam linetype ortho n";
    
      std::vector already_drawn;
      graph += make_class_graph_impl(^^U, already_drawn);
      graph += "@enduml";
    
      return std::define_static_string(graph);
    }

    Here we see the first interesting thing, something called std::define_static_string[2]. What this does is take what is a compile time std::string and create a string literal, which we can return from a consteval function. If we were to try to return the std::string we would be greeted with the compiler error

    :116:24: note: pointer to subobject of heap-allocated object is not a constant expression
    /opt/compiler-explorer/clang-bb-p2996-trunk-20250703/bin/../include/c++/v1/__memory/allocator.h:117:13: note: heap allocation performed here
      117 |     return {allocate(__n), __n};

    Which makes sense as we cannot expect to create an object we allocate on the heap at compile time, then have this object exist in the heap also at runtime. This is why we end up with a big fat literal string that holds our final UML diagram, which we can do whatever we want with in main(). Looking at the assembly you’ll see the end result:

     .asciz  "@startuml nskinparam linetype ortho ntogether {n class "MyClass"n  "MyClass"*--"unsigned int"n  "MyClass"*--"unsigned int"n  "MyClass"*--"unsigned int"n  "MyClass"*--"Nested"n  "MyClass"-up-|>"MyBase"n}ntogether {n class "Nested"n  "Nested"*--"int"n  "Nested"*--"int"n  "Nested"*--"MyClass"n}ntogether {n class "MyBase"n  "MyBase"*--"vector>"n}ntogether {n class "vector>"n}n@enduml"
    

    Now that we have a magic function that “defines a static string” for us, let’s dig into the actual reflection bits. The first thing to note is what we pass to make_class_graph_impl, which shows the use of the lift operator, ^^U. This creates a std::meta::info object that in this case is a reflection of a type. The Impl function itself, as you may have guessed, is meant to be recursive and takes a second argument that we’ll explain later:

    consteval std::string make_class_graph_impl(std::meta::info head, std::vector& already_drawn)
    {
      [...]
    
      constexpr auto ctx = std::meta::access_context::current();
      std::string uml_diagram;
        
      uml_diagram += "together {n class " + add_quotes(display_string_of(head)) + "n";
      
      const std::string indent = "  ";
    
      // members
      for (std::meta::info field_info : std::meta::nonstatic_data_members_of(head, ctx))
      {
        uml_diagram += indent +  add_quotes(display_string_of(head)) + composition_arrow + add_quotes(display_string_of(remove_ptr_cv_type_of(field_info))) + "n";
      }
    [...]

    First let’s talk about the new “context” object: the main paper[1] describes it as “a class that represents a namespace, class, or function from which queries pertaining to access rules may be performed[…]“, and it was introduced in [3] as a mean to resolve the “encapsulation” issue. This context comes in 3 flavours:

    • std::meta::access_context::current(): for accessing the public stuff in the current scope
    • std::meta::access_context::unprivileged(): for accessing public stuff in the global scope
    • std::meta::access_context::unchecked(): for accessing anything in the global scope

    After that the interesting things are std::meta::nonstatic_data_members_of(head, ctx), and std::meta::info::display_string_of(head). Those are pretty self explanatory, and they do make “metaprogramming” as easy as “programming”!

    It gets a bit trickier later, when we have to recurse

    uml_diagram += make_class_graph_impl(remove_ptr_cv_type_of(field_info), already_drawn);

    Besides the very convenient fact that we can keep track of what we iterate over already simply using an std::vector, we do define a custom function with the terrible name of remove_ptr_cv_type_of.

    consteval auto remove_ptr_cv_type_of(std::meta::info r) -> std::meta::info {
      return decay(remove_pointer(is_type(r) ? r : type_of(r)));
    }

    This does what it says on the box, as in our UML we don’t want to distinguish between const/volatile/pointers whathaveyou. The important and interesting bit however is std::meta::is_type(..). This function tells us what kind of info object we have, as they can be reflections of anything, and therefore we need to test whether or not we have a reflection of a type or a value and apply std::meta::type_of only when necessary. This is a bit of a pain, but imho it’s a small price to pay for Reflections.

    That is basically it, we just need to try it out plotting our PlantUML result, which gets printed out at runtime in this case.

    References

    [1] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2996r12.html

    [2] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3491r3.html

    [3] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3547r0.html

    BMI Calculator – Check your Body Mass Index for free!

    Share. Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Telegram Email
    Previous ArticleToday’s NYT Mini Crossword Answers for Sunday, Aug. 3
    Next Article Parsing without ASTs and Optimizing with Sea of Nodes [video]
    TechAiVerse
    • Website

    Jonathan is a tech enthusiast and the mind behind Tech AI Verse. With a passion for artificial intelligence, consumer tech, and emerging innovations, he deliver clear, insightful content to keep readers informed. From cutting-edge gadgets to AI advancements and cryptocurrency trends, Jonathan breaks down complex topics to make technology accessible to all.

    Related Posts

    It’s time we blow up PC benchmarking

    August 29, 2025

    If my Wi-Fi’s not working, here’s how I find answers

    August 29, 2025

    Asus ROG NUC 2025 review: Mini PC in size, massive in performance

    August 29, 2025
    Leave A Reply Cancel Reply

    Top Posts

    Ping, You’ve Got Whale: AI detection system alerts ships of whales in their path

    April 22, 2025166 Views

    6.7 Cummins Lifter Failure: What Years Are Affected (And Possible Fixes)

    April 14, 202548 Views

    New Akira ransomware decryptor cracks encryptions keys using GPUs

    March 16, 202530 Views

    Is Libby Compatible With Kobo E-Readers?

    March 31, 202528 Views
    Don't Miss
    Gadgets August 29, 2025

    U Mobile deploys ULTRA5G in Kota Kinabalu

    U Mobile deploys ULTRA5G in Kota Kinabalu After unveiling its new ULTRA5G network for in-building…

    AKASO Launches Keychain 2: A Pocket-Sized 4K Action Camera Built for Creators on the Move

    Huawei Malaysia beings preorders for Pura 80

    It’s time we blow up PC benchmarking

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo

    Subscribe to Updates

    Get the latest creative news from SmartMag about art & design.

    About Us
    About Us

    Welcome to Tech AI Verse, your go-to destination for everything technology! We bring you the latest news, trends, and insights from the ever-evolving world of tech. Our coverage spans across global technology industry updates, artificial intelligence advancements, machine learning ethics, and automation innovations. Stay connected with us as we explore the limitless possibilities of technology!

    Facebook X (Twitter) Pinterest YouTube WhatsApp
    Our Picks

    U Mobile deploys ULTRA5G in Kota Kinabalu

    August 29, 20252 Views

    AKASO Launches Keychain 2: A Pocket-Sized 4K Action Camera Built for Creators on the Move

    August 29, 20252 Views

    Huawei Malaysia beings preorders for Pura 80

    August 29, 20252 Views
    Most Popular

    Xiaomi 15 Ultra Officially Launched in China, Malaysia launch to follow after global event

    March 12, 20250 Views

    Apple thinks people won’t use MagSafe on iPhone 16e

    March 12, 20250 Views

    French Apex Legends voice cast refuses contracts over “unacceptable” AI clause

    March 12, 20250 Views
    © 2025 TechAiVerse. Designed by Divya Tech.
    • Home
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.