Original Post
So question first, then background: Is there a way I'm not aware of in C++ to have two types defined that cannot be implicitly cast to eachother, but CAN be implicitly treated as integers?
I'm doing a handle-based resource management system in a project and I'm trying to come up with a clever way to get handle generation to be unique and avoid conflicts. Basically if I have two resources A and B, and resource managers ResourceManagerA and ResourceManagerB, I'd like to be able to generate unique handles for each instance in each type. The route I'm going right now is to have a shared integral counter in the ResourceManager base class that is incremented when ANY manager requests a new handle. This works, but obviously if for some reason two resources have the same handle, they could be used incorrectly. An easy example is how I'm wrapping OpenGL's texture handles. The manager requests OpenGL to generate a new texture handle instead of using the shared counter in the base ResourceManager class for that, so there's a discontinuity there and potentially multiple handles with the same integral representation could be made. If I had a way to say "type HandleA is an integer, and type HandleB is an integer, but casting a HandleB to a HandleA will fail at compile-time" that would be perfect...I just don't know how to do it. I'm thinking perhaps using bitfields would work?
I'm doing a handle-based resource management system in a project and I'm trying to come up with a clever way to get handle generation to be unique and avoid conflicts. Basically if I have two resources A and B, and resource managers ResourceManagerA and ResourceManagerB, I'd like to be able to generate unique handles for each instance in each type. The route I'm going right now is to have a shared integral counter in the ResourceManager base class that is incremented when ANY manager requests a new handle. This works, but obviously if for some reason two resources have the same handle, they could be used incorrectly. An easy example is how I'm wrapping OpenGL's texture handles. The manager requests OpenGL to generate a new texture handle instead of using the shared counter in the base ResourceManager class for that, so there's a discontinuity there and potentially multiple handles with the same integral representation could be made. If I had a way to say "type HandleA is an integer, and type HandleB is an integer, but casting a HandleB to a HandleA will fail at compile-time" that would be perfect...I just don't know how to do it. I'm thinking perhaps using bitfields would work?