0

I have a colleague that is not keen on using modern C++

For example when I asked him to start using r_value reference he wouldn't do it. When I asked him to use std::array instead of c arrays (char example[8]) he wouldn't do it. When I asked him to stop doing c casts ( A* a = (A*)b ) he wouldn't do it. You get my point.

Usually I can find references of Scott Meyers or Bjarne Stroustrup of why modern C++ shouldn't be used this way, and then he cannot debate it anymore.

Unfortunately I cannot find a direct reference of anyone why is bad practise to use "unsigned" instead of "unsigned int". I managed to make me him used "singed" before "int but I cannot convince him to use "unsigned int".

So my question is should we use "unsigned int" instead of "unsigned"?

  • 3
    The question per se is fine, but do we really need the story about your colleague? – BiagioF Mar 29 '19 at 14:26
  • No ok, you are right. Is it better? –  Mar 29 '19 at 14:28
  • 1
    While there are good reasons for using rvalue references and not using plain arrays, I think when it comes to `unsigned` vs `unsigned int`, we're really just talking about a matter of style and personal preference. The only argument I could think of would be that `unsigned int` is more explicit and (probably) more common, which is not really all that strong an argument… – Michael Kenzel Mar 29 '19 at 14:30
  • Possible duplicate of [Difference between unsigned and unsigned int in C](https://stackoverflow.com/questions/7176657/difference-between-unsigned-and-unsigned-int-in-c) – Pavan Manjunath Mar 29 '19 at 14:30
  • `unsigned` and `unsigned int` are exactly equivalent. It's entirely a question of preference, readability and consistency. There aren't really any strong argument either way. Just use whatever is already being used in the project you are working on. – François Andrieux Mar 29 '19 at 14:31
  • Technically there is no difference between these two. Personally I use `unsigned int` because its clearer for anyone who will see my code later. It's like I see `unsigned` and I can only wonder is it `unsigned int`?, is it `unsigned char`?, etc. But that's just me. – DimChtz Mar 29 '19 at 14:31
  • 4
    Neither, use `uint32_t` obviously. It's shorter, hence faster. – Barry Mar 29 '19 at 14:31
  • The thing is we are cross platform developers, so release with different compliers too. A weird thing we are using on android takes for example "int" as "unsigned int" instead of signed. So I like things as clean as possible. –  Mar 29 '19 at 14:32
  • You said: *I cannot find a direct reference of anyone why is bad practice to use "unsigned" instead of "unsigned int"*, how can you tell if it is a good practice or not then? – Raydel Miranda Mar 29 '19 at 14:32
  • 1
    What do you mean when you say "use `int` as `unsigned int`". These are two different types with different behavior!? – Michael Kenzel Mar 29 '19 at 14:32
  • You can! int is signed int. –  Mar 29 '19 at 14:34
  • @C.Michael I'm sure `int` is required to be `signed`. If it's not, the compiler you are using is non-conferment so who knows what advice is applicable or not. In c++ `int` is `signed` by default. – François Andrieux Mar 29 '19 at 14:34
  • @FrançoisAndrieux that happened on our android project. I had to define something not exactly sure to make that int = singed int and not unsigned int –  Mar 29 '19 at 14:35
  • `unsigned int` decreases the amount of meaningful code you can fit in 80 characters. – DaBler Mar 29 '19 at 14:36
  • @C.Michael Then you aren't asking for advice about c++. You're asking for advice for your specific c++-like language. In either case, if `unsigned int` and `int` are identical in your case, all the advice here holds. If not, then there isn't really a question in the first place. If they are different use the construct that matches what you need. – François Andrieux Mar 29 '19 at 14:36
  • `signed` is the same as `int` is the same as `signed int`. And `unsigned` is the same as `unsigned int` [\[dcl.type.simple\]](http://eel.is/c++draft/dcl.type.simple#tab:simple.type.specifiers) – Michael Kenzel Mar 29 '19 at 14:38
  • The projects are cross platform. What I want is for the code to be absolutely clear, It is my preference that we write "unsigned int" is there a reason you should do that or not? NO is a fine answer. –  Mar 29 '19 at 14:39
  • Opinion: If you are writing cross platform code, you should give [fixed width integer types](https://en.cppreference.com/w/cpp/types/integer) a types a good look-over. Sucks when the code works because `int` is 64 bit on architecture A and fails because it's 16 bit on architecture B. – user4581301 Mar 29 '19 at 14:40
  • 1
    The fact that it's cross-platform doesn't matter here. All conforming C++ compilers *must* behave the same when it comes to the meaning of simple type specifiers… – Michael Kenzel Mar 29 '19 at 14:41
  • @user4581301 note that the fundamental integer types all have guaranteed minimum ranges. I would first rely on that and only use fixed-width integer types in code that really relies on a type having precisely the right number of bits rather than just enough bits… – Michael Kenzel Mar 29 '19 at 14:44

1 Answers1

3

I cannot find a direct reference of anyone why is bad practise to use "unsigned" instead of "unsigned int"

That's probably because it isn't a practice that's considered bad by the majority (to my knowledge).

unsigned and unsigned int are exactly the same type, just as signed, signed int and int are. I don't know of a case where using one over the other would have surprising results.

Given that the choice is primarily a matter of taste. Arguing about the choice is bike-shedding.

eerorika
  • 232,697
  • 12
  • 197
  • 326