typedef and size_t

Hello,

I don't understand why this doesn't compile. It returns an error saying that max(long unsigned int, size_t) does not exist.

1
2
3
4
long unsigned int a = 1;
size_t b = 2;

std::max(a, b);


Since size_t is a typedef for unsigned long int shouldn't this be ok?
Last edited on
According to my implementation:
typedef unsigned int size_t;
Typedef - d types cannot be used "directly" with the types they replace. You will need to cast them.

Both of these compile:
1
2
3
4
long unsigned int a = 1;
size_t b = 2;

std::max((size_t)a, b);


1
2
3
4
long unsigned int a = 1;
size_t b = 2;

std::max(a, (long unsigned int)b);
That's not the type it replaces. I'm sure it works if you use the correct type, since a typedef is just another name for a type:

1
2
3
4
unsigned int a = 1;
size_t b = 2;

std::max(a, b);
That's not the type it replaces. I'm sure it works if you use the correct type, since a typedef is just another name for a type.

That's what I thought. But according to my implementation:

1
2
3
4
//file /usr/lib/gcc/i486-linux-gnu/4.4/include/stddef.h

#define __SIZE_TYPE__ long unsigned int
typedef __SIZE_TYPE__ size_t;


Is size_t defined somewhere else and I am looking to an old file?
Last edited on
Okay, here we go.
From a working draft of ISO/IEC 9899:1999, 7.17.2:
size_t
which is the unsigned integer type of the result of the sizeof operator

Well, here _SIZE_T is just renamed with typedef directive to avoid typing the cast over and over.
Probably due to the command range without introducing a new type nor a range.
size_t is compatible with any integer defined in the C library as an integral type.
It has few relations to the classification but more to a machinery under
the source text.


Topic archived. No new replies allowed.