I want to declare my own numeric types, exactly like unsigned int, but I do not want the types to be implicitly converted. I tried this first: typedef unsigned int firstID; typedef unsigned int secondID;
but this is no good as the two types are just synonyms for unsigned int, so are freely interchangable.
I'd like this to cause an error:
firstID fid = 0;
secondID sid = fid; // no implicit conversion error
but this to be okay:
firstID fid = 0;
secondID sid = static_cast<secondID>(fid); // no error
My reason is so that function arguments are strongly typed, eg:
void f( firstID, secondID ); // instead of void f(unsigned int, unsigned int)
What is the mechanism I am looking for?
Thanks
Si
Overview. Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.
Which type of conversion is NOT accepted? Explanation: Conversion of a float to pointer type is not allowed.
Type Casting is basically a process in C in which we change a variable belonging to one data type to another one. In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do.
The two types of type casting in C are: Implicit Typecasting. Explicit Typecasting.
Maybe BOOST_STRONG_TYPEDEF form boost/strong_typedef.hpp would help.
As you noted: typedef is badly named (it should be typealias (D has explicitly added typealias (last time I looked))
So the only way you can do this is to create two unique classes.
I am not going to say you can't write a specialization of static_cast<> to do what you want, but I think (and I have not put that much thought into it yet) doing so would be a bad idea (even if it legal), I think a better approach is to have each class constructor use unsigned int that are explicit (so there is no auto conversion).
struct FID
{
explicit FID(unsigned int v): value(v) {}
operator unsigned int() {return value;}
unsigned int value;
};
class SID {/* Stuff */};
FID fid(1U);
SID sid(2U);
doSomthingWithTwoSID(sid,SID(static_cast<unsigned int>(fid));
Making the constructor explicit means no auto conversion between types.
By adding the built in cast operator to unsigned int means that it can be used anywhere an int is expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With