What do you gain from this? If you think you will have some gain you need to justify it.
Usually this is used when you really need something with no signal, that it is fundamental that it be so. who needs the 4 billion he can get. Another use is when clearly the numbers can not have negatives.
Not everyone understands everything that has to be careful to use unlabeled types (so much that * warnings are generated in some cases, but not at all). Why use something different?
Using a type that is not flagged by itself is not a big problem ( but it is too ). The problem begins when you merge the flagged with the flagged ones. And the libraries require more use of the flagged type, so it will blend. Before you start mixing you have to know how to do the conversions, you have to be sure to always be aware to avoid problems of the type shown in this question . There's no shortage of problem examples .
Optimization
Processors are optimized to work with int
. The most obvious semantics with numbers is what int
offers.
In fact, if you think about it, it would make even more sense to use a char
in the example shown (up to 255 that is sufficient and takes up only 1 byte). Why do you need a type that reaches 2 billion if you only need 3 or more? They are certainly few because they are using switch
. On the other hand it fits the question again, what is the gain?
In some architectures a char
may be slower than int
.
Some people think they will gain memory by using char
instead of int
, but it is common for automatic padding to align memory and consumption ends up the same.
In some cases there may be certain optimizations when the type is not flagged. If you need this you have to understand when it occurs and know when its use can be useful and will not cause other problems.
There is a lot of use of unlabeled types
Bit operations (masks, sizes and some counters, combination of data, specific representations segmented as date / time, etc.) usually work best with unlabeled types. But most problems do not have to deal with it.
In fact experienced programmers often prefer a int32_t
in a lot of situations since they have fixed size on all platforms and give more control to what they are doing. But it may have some minimal performance loss. int
is to be the most performative type of the platform with a minimum guaranteed size (16 bits), but it can be larger and on modern platforms it is usually (32 bits). But some style guides "prohibit" the use of these types. There must be some reason.
In other cases, they prefer size_t
which is an unsigned type. Note that this type is often used in real cases. If you look at most of the answers here about C or C ++ you use int
because it works, but in real cases the type not flagged (with modifier as the question calls it) is even used, when it makes sense. Where to see it:
for (int i = 0; i < array.length(); i++) {
//algo aqui
}
In professional codes, when it makes sense it's usually written like this:
for (size_t i = 0; i < array.length(); i++) {
//algo aqui
}
When you see beginner or lay codes, you may be suspicious. But if you see experienced programmers doing something in a way they know that's the best. I'm not saying you should not question, on the contrary, you need to understand why.
What I see a lot is the naive programmer does not understand all kinds nuances and use the easiest and obvious without thinking of all the implications. This is what happens in most simple examples in exercises. Producing real C / C ++ code usually forces you to think a little more about types.
Conclusion
I believe (field of opinion) that many people, especially in exercises, use without thinking, without knowing that it can be different or just because it seems shorter to use the type without modifier. The others use it because they know it's the best option in that situation.
One reason might be lack of unanimity . You have who only sees problems .
Type documentation .
Less known lesser types .