
On Mon, Aug 18, 2008 at 4:05 PM, Daryle Walker
Have you ever read a thread in a programming news-group, where some newbie asks how to implement some wacky scheme? Several far-out solutions come forth, then flame-wars erupt over the pros & cons. Then finally someone asks what the newbie really needs and gives a completely different solution based on that response. Basically, the newbie was trying to do something in a manner s/he shouldn't even thought of, let alone suppose it was good enough to try implementing. This is one of those times.
On Aug 16, 2008, at 1:32 PM, Zeljko Vrba wrote:
Integer types in C and C++ are a mess. For example, I have made a library where a task is identified by a unique unsigned integer. The extra information about the tasks is stored in a std::vector. When a new task is created, I use the size() method to get the next id, I assign it to the task and then push_back the (pointer to) task structure into the vector. Now, the task structure has also an "unsigned int id" field. In 64-bit mode,
sizeof(unsigned) == 4, sizeof(std::vector::size_type) == 8
I get a warnings about type truncation, and obviously, I don't like them. But I like explicit casts and turning off warnings even less.
What are you suggesting, that in 64-bit mode std::vector::size_type shouldn't be 64-bit? Do you not want a warning when you convert from 64-bit to 32-bit type? One solution to your problem is to wrap the vector into another interface which communicates in terms of functions that are specific for your needs. That interface can do the conversions, and you have the added benefit that user code can't use the full spectrum of std::vector functions, which presumably could make it possible to change the underlying container altogether. By the way, you don't know for sure that sizeof(unsigned) is 4. Strictly speaking, you don't even know that the value returned by sizeof() is in 8-bit bytes. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode