5

There are some types in Qt, for example quint8 that is guaranteed to be 8-bit on all platforms supported by Qt.

I am wondering if C++11 has such kind of type? if not, what is the alternative for this?

Thanks.

László Papp
  • 51,870
  • 39
  • 111
  • 135
camino
  • 10,085
  • 20
  • 64
  • 115

2 Answers2

14

Yes, C++11 adds types with more precisely defined sizes. Here's the reference.

They are defined in <cstdint>.

You are guaranteed to have these:

intmax_t        uintmax_t

int_least8_t    uint_least8_t
int_least16_t   uint_least16_t
int_least32_t   uint_least32_t
int_least64_t   uint_least64_t

int_fast8_t     uint_fast8_t
int_fast16_t    uint_fast16_t
int_fast32_t    uint_fast32_t
int_fast64_t    uint_fast64_t

You may or may not have these:

int8_t          uint8_t 
int16_t         uint16_t
int32_t         uint32_t
int64_t         uint64_t

intptr_t        uintptr_t

Explanations:

  • The u prefix means unsigned.
  • The least variants are the smallest integer types available with at least that width.
  • The fast variants are the fastest integer types available with at least that width.
  • The intptr variants are guaranteed to be convertible to void* and back.
  • The max variants are the largest available types.
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Lstor
  • 2,265
  • 17
  • 25
  • what does fast really mean? – camino Jun 11 '14 at 18:44
  • 1
    @camino: "At least as fast as any other integer type with at least the specified width." – Lstor Jun 11 '14 at 18:45
  • 1
    @camino That the type only guarantees a certain minimum length, but if on the given platform a longer type is "faster" to deal with, it will be used instead. – Kuba hasn't forgotten Monica Jun 11 '14 at 18:46
  • The `intprt` variants are not *compatible* with `void*`; type compatibility is a much stronger condition. The guarantee is that converting a `void*` value to `intptr_t` or `uintptr_t` and back to `void*` yields the original `void*` value (i.e., there's no loss of information). Both types are optional. – Keith Thompson Jun 11 '14 at 18:59
  • 1
    From what I know, compilers aren't guaranteed to have those. They are only required to have `typedef`s of those form, not those specifically. Unless something has changed in the standard. – Cole Tobin Jun 11 '14 at 19:17
  • @ColeJohnson: That is correct, but do you think that matters for OP? – Lstor Jun 11 '14 at 19:18
  • 1
    I'm saying your claim that "you are _guaranteed_ to have" (emphasis mine) is wrong. – Cole Tobin Jun 11 '14 at 19:19
  • @ColeJohnson: You are guaranteed to have a typename in `` with each of those names. I'm not saying that they are separate types, just that they are present and can be used. I don't think it's relevant whether it's a separate type or a `typedef`, but maybe I'm missing your point? – Lstor Jun 11 '14 at 19:21
  • What I personally do not understand in this answer is the brain dump of all the types instead of just focusing on the question of quint*8*. I think from the question's point of view, all the rest is red-herring and needless noise. Also, it is worth noting, as per my answer, that this will not work on all the platforms that Qt supports. – László Papp Jun 12 '14 at 07:48
  • 1
    @FinalContest: I respectfully disagree, and read the quint*8*-part as an example rather than the complete list of what he needs. Besides, listing all types is more useful for future googlers. Since OP explicitly asks if C++11 adds support for such types, I presume that he is aware of the limitation and has support for C++11. – Lstor Jun 12 '14 at 08:40
  • The question was `Does c++11 have something like quint8?`. If someone asks about shared pointers, you do not start talking about the scoped, unique, auto, weak, borrowed and other pointers because the majority of that would be red-herring, and hence the signal/noise ratio is very low to get the useful information out. What people usually do on SO if they want to provide details is to give a link to the further details. How max or intptr is relevant to quint8 or even quint16/32/64, I do not know. (Btw, the OP is also saying "type similar to quint8' and "all platforms suppoted by Qt") – László Papp Jun 12 '14 at 08:58
1

Yes, it does, and even more. From the documentation:

uint8_t unsigned integer type with width of 8
uint_fast8_t fastest unsigned integer type with width of 8
uint_least8_t smallest unsigned integer type with width of at least 8

Disclaimer: this will not obviously work on platforms where Qt is supported and does not have C++11. If you plan to support those, stick with your quint8, otherwise go drop it in favor of modern C++.

Make sure you will have this in your qmake project file to actually enable C++11:

CONFIG += c++11
László Papp
  • 51,870
  • 39
  • 111
  • 135