Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector<bool> optimization implementation

In the documentation, I can see that std::vector<bool> is optimized for space-efficiency by making every boolean occupy one single bit. From the documentation:

The manner in which std::vector is made space efficient (as well as whether it is optimized at all) is implementation defined.

Does this mean that it depends on the compiler's implementation? If it does, where can I check if my compiler supports it? Why wouldn't they want it supported? It seems like a really simple and efficient implementation.

If not, what does it mean and what does it imply if I want this optimization to take place?

I'm using TDM GCC toolset.

like image 591
Simon Avatar asked Jul 11 '16 04:07

Simon


2 Answers

Implementation defined means it depends on what constitutes the parameters of the abstract machine. (I.E., the algorithms which define your host operating system, their implementation specification, and system calls). An informative Q&A on what "implementation defined” means is here.

More than likely, if you have a modern machine with a modern compiler/IDE, it supports implementation definition.

If a compiler doesn't support it, it's not likely because they don't "want" it, but because either it is a very old compiler, or a very resource limited machine.

It boils down to it's machine dependent; so different operating systems will handle accomplishing it it their own way. (i.e., 32-bit vs 64-bit, etc.) It doesn't effect the portability, unless working with an (very much) older compiler. You can check the compiler version's specifications if it's a concern, which is easily found online by searching for your compiler and its version.

like image 74
NonCreature0714 Avatar answered Oct 17 '22 21:10

NonCreature0714


The formal language definition doesn't want to exclude reasonable implementations, so they always have to be a bit careful.

For instance, a typical debug build is still Standards-conforming, but I could very well see a vector<bool> not being compressed in debug mode.

Now this is not unspecified but implementation defined. That means the fact whether it's compressed should be in the compilers documentation somewhere, but the Standard doesn't describe how the documentation should be organized.

If your compiler doesn't support it as you'd like, you can just use another library (Boost being the obvious candidate). vector<bool> typically isn't a class that depends on deep compiler magic, so alternatives are easy to write.

like image 20
MSalters Avatar answered Oct 17 '22 23:10

MSalters