c++ stl bitset only useful for known-at-compile-time number of bits

Found in the libstdc++ docs:

Extremely weird solutions. If you have access to the compiler and linker at runtime, you can do something insane, like figuring out just how many bits you need, then writing a temporary source code file. That file contains an instantiation of bitset for the required number of bits, inside some wrapper functions with unchanging signatures. Have your program then call the compiler on that file using Position Independent Code, then open the newly-created object file and load those wrapper functions. You’ll have an instantiation of bitset for the exact N that you need at the time. Don’t forget to delete the temporary files. (Yes, this can be, and has been, done.)

Oh yeah – feel the love.

Brought to you by the stl-is-often-worse-for-you-than-meth dept.

1 thought on “c++ stl bitset only useful for known-at-compile-time number of bits

  1. Sounds like a fun solution… I wonder if anyone has felt compelled to put together a CFront style filter with libtcc to be able to do that without needing any temporary files – it is already possible to use libtcc to target in memory (no-PIC) ready to run object code.
    If possible, then imagine having a small binary bootstrap which would pull in the source to run the program. A real “open source” system.
    For a taste of this – you can download an ISO where it boots Linux, building the kernel from source as part of the boot process in less than 3 minutes.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.