🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Data type diversity

Started by
51 comments, last by Calin 1 year, 10 months ago

frob said:
The two most critical are conversion rank and the conversion between types.

There's also std::vector<bool> but it seems consensus nowadays is to avoid that particular specialization…

Advertisement

Oberon_Command said:

Calin said:
you will probably say there is no connection between templates and templars

Correct, there is no connection. A template is just a way to write generic code.

one way or the other it looks like you have nothing against me going down this path so I`ll take that as a tacit admission of my request.

My project`s facebook page is “DreamLand Page”

Calin said:

Oberon_Command said:

Calin said:
you will probably say there is no connection between templates and templars

Correct, there is no connection. A template is just a way to write generic code.

one way or the other it looks like you have nothing against me going down this path so I`ll take that as a tacit admission of my request.

Why would you think I would? I don't understand that post or why you're asking for permission.

Oberon_Command said:
I don't understand that post or why you're asking for permission.

I learn by association. I`m associating terms with people. Templates is a pattern well understood by the people who take templars for granted. I`m not sure why. It could be that the two terms are close to each other as neuronal nodes in the brain. That`s a wild speculation but I can`t come up with something better as explanation. My reasons could be absolute nonsense but I can`t move further without an explicit yes. Resemblence between words is how I learn. So if my explanation doesn`t seem to make sense could you at least say yes as favor to a person in regards to which you hold nothing against?

At the end of the day it doesn`t really matter why I can`t use it the point is I can`t use it without explicit permission. You have a knowledge base that is yours and it can`t be easily accessed by outsiders.

My project`s facebook page is “DreamLand Page”

@Calin Nobody should need to affirm your mnemonics. That is crazy talk.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

A bool is effectively an enum { false, true }. The reasons for using bools are basically the same as the reasons for using enums, and you should be using enums all of the time.

(Note that there is no guarantee that a bool is only one byte in size. A bool can be four bytes, for example. You should still use bools wherever they make sense, even though they are potentially less efficient than chars. But if you have a big container of them, it might make sense to makes use a char-sized enum instead, for efficiency.)

vector bool is very handy for voxel storage.

a light breeze said:
(Note that there is no guarantee that a bool is only one byte in size. A bool can be four bytes, for example. You should still use bools wherever they make sense, even though they are potentially less efficient than chars. But if you have a big container of them, it might make sense to makes use a char-sized enum instead, for efficiency.)

Neigther is it guaranteed for a “char” to be exactly one byte (*) . Quoting the standard:

Besides the minimal bit counts, the C++ Standard guarantees that

1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long).

Note: this allows the extreme case in which bytes are sized 64 bits, all types (including char) are 64 bits wide, and sizeof returns 1 for every type.

If you want exact sizes, use uintX_t. And/or static_assert(sizeof(bool) == 1) could be handy if you rely on this behaviour.
(*) I should probably clarify, yes a char would be one byte in the quoted scenario as well as the byte-size is 64 bits, but in that case clearly char is the equivalent of 8 bytes from our understanding, and it goes to reason that on such a system where char==64 bits, then a bool would probably/likely not be a multiple of that. Similarily, a bool in real-world scenarios would probably only be larger than one byte if it actually makes some sort of sense to the hardware.

taby said:
vector bool is very handy for voxel storage.

Maybe, but the existance of std::vector<bool> as it is fucks over lots of generic code that you'd want to write to deal with std::vector<X>. I had to write my own vector-class because dealing with vector<bool> meant my type-systems dynamic array-handling could not be fully generalized (as the vector<bool> had to be treated differently pretty much all the time). There's nothing wrong with havina vector<bool> - but they should have made it a separate class, like a dynamic_bitset, and not force people to deal with the weirdness of one type of vector being different.

a light breeze said:
You should still use bools wherever they make sense, even though they are potentially less efficient than chars.

Huh, you really think compiler implementors will pick a less efficient representation? I don't think so. The C++ standard doesn't have many restrictions on internal representation, it only says how it must behave. A compiler author can pick an optimal machine-representation for implementing a boolean and eg perform conversions at the right spot to make is obey the rules.

a light breeze said:
But if you have a big container of them, it might make sense to makes use a char-sized enum instead, for efficiency.)

a 32 or 64 bit integer is far more efficient (by a factor 8 just for storage, more if you want to perform operations on your bits as a cpu can handle then 32 or 64 bits in a single instruction, read about “bit twiddling” for more information). For more bits, use a bitset, which afaik is simpley std::vector<bool> (but never tried that).

Alberth said:
For more bits, use a bitset, which afaik is simpley std::vector (but never tried that).

It is not. bitset's size must be known at compile time, which makes it great for use with enums to name the bits or you don't want heap allocations, but less great if you truly need a dynamic bitset.

It's also possible that adding a char (or a bool) will cause padding bytes to be added to your structure to preserve alignment, so adding a bool could in some cases bloat your structure if not carefully placed. This is one reason I prefer bitsets for groups of bools.

This topic is closed to new replies.

Advertisement