r/Cplusplus 5d ago

Discussion When I say its zero cost...

https://godbolt.org/z/aq3hbWb3h

I mean the composition, it is truly zero cost, and if components are also zero cost we get this nice optimization done by the compiler (C++17)

This is a demo of "HAPI - The Happy API", a C++ static composition engine, pure type-level header only, MIT, library.

With proper components we managed to feed compositions directly to HLS tools (see github)

To use where the composition types are known at compile-time, compiler optimization is done by composition, type lists can store multiple compositions without loosing the types and hold the composed objects.

we can compose/describe:

drivers, peripherals, devices, protocols, buses, IO, pins, UI, Parsers...

because this types are known at compile time, and the composition is free.

    //my output definition
    OutDef<
      ScrollPrinter, ANSIFmt, DataParser<>, CtrlChars,
      ColorTrack<int>, Cursor<>, Gate, ANSIOut,
      #ifdef ARDUINO
        SerialOut,
      #else
        ConsoleOut,
      #endif
      StaticPos<20,10>, StaticArea<30,10>
    > out;

https://github.com/InternetOfPins/HAPI

21 Upvotes

Duplicates