Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned long long conflict with uint64_t? [duplicate]

Tags:

c++

We use template specialization for some type parameter like

class my_template_class<uint64_t M>: public my_template_class_base<uint64_t> {
 ....
}

class my_template_class<unsigned long long,M>: public my_template_class_base<unsigned long long> {
 ....
}

This is working perfectly with 64-bit compilation with gcc. While when we try the 32 bit mode, it reports "previous definition" for above two classes.

So unsigned long long is the same as uint64_t in the 32-bit compilation but not in 64-bit compliation?

The compilation difference is the CXX flag -m32 and -m64

like image 505
thundium Avatar asked Aug 25 '15 07:08

thundium


2 Answers

So unsigned long long is the same as uint64_t in the 32-bit compilation but not in 64-bit compilation?

Yes.

In 32-bit mode, most likely long is 32 bits and long long is 64 bits. In 64-bit mode, both are probably 64 bits.

In 32-bit mode, the compiler (more precisely the <stdint.h> header) defines uint64_t as unsigned long long, because unsigned long isn't wide enough.

In 64-bit mode, it defines uint64_t as unsigned long.

It could have defined it as unsigned long long in both modes. The choice is arbitrary; all that's required is that it has to be a 64-bit type.

In general, each of the integer types defined in <stdint.h> is a typedef for some predefined type with the appropriate characteristics. You can't assume that any of them are distinct from the predefined types.

like image 80
Keith Thompson Avatar answered Sep 29 '22 14:09

Keith Thompson


This is from stdint.h for GCC 4.8:

#if __WORDSIZE == 64
typedef unsigned long int   uint64_t;
#else
__extension__
typedef unsigned long long int  uint64_t;
#endif

So:

So unsigned long long is the same as uint64_t in the 32bit compliation but not in 64 bit compliation?

Yes.

like image 37
Paolo M Avatar answered Sep 29 '22 15:09

Paolo M