1

So I stumbled upon When should I use the C++ fixed-width integer types and how do they impact performance? and Should I use cstdint? where the advantages and disadvantages of fixed width integer types as defined in <cstdint> are listed.

I kinda do like to encode the intended range of a variable into its type but I also don't want to enforce the CPU to do extra operations just to use a uint16_t instead of a plain int when I am not strictly required to have a variable holding exactly 16 bits.

I also read about types like std::uint_fast16_t and so on. From my understanding using this type should ensure that I am guaranteed to be able to store a 16-bit number in that variable but I should never have to pay any runtime penality for using this type since on every architecture where e.g. uint32_t would be faster, that would be used automatically for me.

This leaves me with the question: Aside from the case that I really need a variable of exact bit width, are there any disadvantages of using std::uint_fast16_t instead of say unsigned int?


EDIT: This is of course assuming that memory consumption is not an issue. If it was, I would use std::uint_least16_t instead.

Raven
  • 2,951
  • 2
  • 26
  • 42
  • " should never have to pay any runtime penality for using this type" is more like " usually not have to pay any runtime penalty for using this type" – chux - Reinstate Monica May 05 '21 at 14:34
  • @chux-ReinstateMonica How is that? Would that then be a bug in the C++ implementation on that particular machine or something else? – Raven May 05 '21 at 14:39
  • 2
    "_... there any disadvantages..."_ that you might use twice the expected space when `uint32_t` is faster. – Richard Critten May 05 '21 at 14:39
  • @RichardCritten Yep fair point - Let me clarify that in the question... – Raven May 05 '21 at 14:41
  • TL;DR from the duplicate: `uint_fast16_t` may not be exactly 16 bits big. It will be the 'fastest' type with *at least* 16 bits. – Adrian Mole May 05 '21 at 14:48
  • 1
    @AdrianMole This question already explains the difference between the two. I don't think the duplicate is accurate. This question is asking if there are other disadvantages to using the `fast` type other than the extra size. – François Andrieux May 05 '21 at 14:49
  • @AdrianMole I also think that the linked question does not answer my question. – Raven May 05 '21 at 14:50
  • 1
    A difference does not have to be a disadvantage. One car is red the other is yellow. That's a difference but not necessarily a disadvatage – Raven May 05 '21 at 14:52
  • 1
    OK - I've reopened after the convincing weight of arguments. :-) – Adrian Mole May 05 '21 at 15:03
  • 2
    @AdrianMole Questions about pros/cons of one type versus another in C/C++ is tricky, so I appreciate your pro/con thoughts about duplication. We could have a single question about the many types versus the other many types. So many things to consider and thus too broad Some issues apply to specific cases, some do not. Short making a treatise "what every programmer needs to know about type selection". Perhaps we need a Q&A to serve as a table of contents linking to the specific cases? – chux - Reinstate Monica May 05 '21 at 16:03

1 Answers1

4

are there any disadvantages of using std::uint_fast16_t instead of say unsigned int.

One disadvantage: uncertain type due do the usual promotions. Does uint16_fast_t convert to a signed or unsigned?

uint16_fast_t fa = 1;
unsigned un = 1;
int i;

fa some_operator i --> may result in an `int` or `uint16_fast_t`
un some_operator i --> result is unsigned.

The ambiguity may negatively affect more complicated equations and behavior on overflow.


IMO, uint16_fast_t only useful in narrow controlled code, not for a general performance improvement. Be careful of Is premature optimization really the root of all evil?.

Many factors affect this conclusion, yet generally for performance, usually best to go for clarity and type unsigned.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256