Found solution :) The proper way is to cast to uintptr_t. This of course required me to change int to uint32_t/uint64_t but since it's only Id, doesn't really matter :)
1 2
some_t x;
uint32_t y = reinterpret_cast<uintptr_t>(x);
But even if I'll use a function, I still have to do the conversion in that function somehow and the code I wrote is very very ugly :/
Yeah but that's the whole point of writing classes and functions anyway...
Take a look in std::vector, it's not very fancy but the API is beautiful.
Found solution :) The proper way is to cast to uintptr_t. This of course required me to change int to uint32_t/uint64_t but since it's only Id, doesn't really matter :)
If that's good enough for you...
i'd write a function that returns the id of everything
First, a small remark: I don't want to use address of something as id, I want to use value of pointer as id.
Second, regarding readability: the code I have is already in a function, check my comment above:
some_t x is actually function parameter :)
My question was about the cast, not about how to put the code in a function, so I think your quest to fix my code was not really necessary.
And finally, your code will only work if size_t is equal or greater than the size of pointer, which (I can assure you) is not always true. On the contrary, uintptr_t is guaranteed to have size big enough to store a pointer.
First, a small remark: I don't want to use address of something as id, I want to use value of pointer as id.
I think I don't understand, that's the same in my opinion...
1 2 3 4
int i = 5;
int* p = &i;
std::cout << p << std::endl;
std::cout << &i << std::endl;
And finally, your code will only work if size_t is equal or greater than the size of pointer, which (I can assure you) is not always true.
size_t is allways big enough to hold any pointer value in your system.
standard library reference wrote:
It is a type able to represent the size of any object in bytes: size_t is the type returned by the sizeof operator and is widely used in the standard library to represent sizes and counts.
The size of an object may be as big as your RAM and the biggest possible pointer value is the size of your RAM - 1 therefore size_t is allways large enough.
My question was about the cast, not about how to put the code in a function, so I think your quest to fix my code was not really necessary.
Sorry if I offended you by trying to give you tips on how to make your code more readable.
Second, regarding readability: the code I have is already in a function, check my comment above:
some_t x is actually function parameter :)
Well, wrapping it in a function makes the code in the function more readable and less ugly.
If you just cast it and someone reads it he'll think "What's happening?"
If you wrap it in a function and just call id(val) it will be clear what you are doing.
The size of an object may be as big as your RAM and the biggest possible pointer value is the size of your RAM - 1 therefore size_t is allways large enough.
Not actually always the case in systems using segmented memory. Actual ponter might be larger than size of largest object put in memory (16bit size_t vs 32bit pointer).
So cast to uintptr_t is a good idea. And also you can change your ID to be uintptr_t too: it is an integer in the end.
Still I do not see the need of pointer at all. Why don't you store all in integers, as you said you do not actually need to handle addresses? Or just use pointer as ID without casting around?
I probably don't understand what Beju is trying to do, and I know this has already been marked as solved, but just in case this might help: If you want a unique ID for any generated thing, I would go back to using a function, you could call it get_unique_ID(), and inside it, you'd have something like