OneOffDriveByPoster @ Wed Nov 05, 2008 10:22 pm wrote:
The point is to have compile time type checking. A container object can also be optimized for each specific type that the template is instantiated for based on the either the primary (general) template or on a specialized (more suitable) template. vector<bool> is a wonderful example of how templates can be specialized to provide better efficiency. Yes, templates can be hard to debug, but then the upcoming C++ standard is meant to make that easier.
But when you are just passing the data to the container via pointer, there does not need to be any type checking.
And since the container only has to hold a pointer, there is no way it can optimize the data.
But I also know that vector<bool> does not save any space. The sizeof(bool) is a char in Java and is a short in C#, and C++ for MSVC. That is because the hardware is optimized for shorts. Chars take longer, and bitfields take 10 times as long. That is because since MMX, registers are totally virtual. A 32 bit register may simply be some portion of a much larger register. So calculating bitfields can take many dozens of instructions.
Quote:
In a C library, you might have to pass the size of the stored object for example. You can use the C++ containers with pointer types anyway.
The only reason a C library would need to know the size would be if you were not using pointers, and needed to sequentially index through.
And yes I agree that you can use pointers in C++ containers. The point is why would we want to use anything else?
Quote:
extern "C" works wonders. Linux has the Itanium ABI (not API) for C++. Most compilers provide bit-packed structs.
Microsoft used Common Language Runtime libraries to convert API calls between different languages. That is not just simple entry points like with libraries, and data needs extensive marshaling. For example, C# uses strings that have a prepended length in from of them, because it is actually derived from Visual Basic, with its variant string. The C++ wrapper class for it is _bstr_t.
So the .Net framework would freak out if C# ever got a null terminated string, and .Net uses unicode.