Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is int guaranteed to be 32 bits on each platform supported by Qt, or only qint32?

Tags:

c++

types

int

size

qt

I remember reading somewhere that Qt guarantees the size of some data types on supported platforms. Is it that int will be at least 32 bits everywhere, and qint32 will be exactly 32 bits everywhere? Or something else?

C++ guarantees that int will be at least 16 bits, and some Qt structures like QRect and QPoint use int internally. I'm developing an application where 32 bits is needed with those types, and I don't want to have to duplicate their functionality so I can use a larger type.

like image 644
Jake Petroules Avatar asked Jun 21 '10 18:06

Jake Petroules


1 Answers

The size of an integer type is up to the compiler. I don't think there's a guarantee that plain int will be of a precise size. But you can make sure you know it's not what you want by adding this line to the beginning of your main():

if(sizeof(int) != 4) {
  throw std::runtime_error("int is not 32-bit");
}
like image 76
Stephen Chu Avatar answered Nov 02 '22 18:11

Stephen Chu