im trying to write a uint128_t library (http://stackoverflow.com/questions/6119306/operator-overloading-c-placement) and ive hit a bump: i need to operate on different types of ints, such as uint128_t ^ uint64_t. i have written the operator overloads such as template <typename t> uint128_t operator^(T val). i also need to be able to do uint128_t ^ uint128_t. however, since my uint128_t uses 2 uint64_t s to store the values, i cant simply use uint128_t as T. thus, i have written 2 different functions, one with T as the argument type and the other with uint128_t as the type.
the problem is that the compiler is using the T val version for both standard c++ int types and uint128_t. how do i get the compiler to differentiate between them?
edit: i have this code in the class in the link:
template <typename T>
bool operator==(T val){
return (LOWER == (uint64_t) val);
}
bool operator==(uint128_t val){
return ((UPPER == val.upper()) && (LOWER == val.lower()));
}
if i do
uint128_t a(5), b(123, 45);
uint64_t c = 6;
(a == c);
(a == b);
both lines would use the top operator. however, since uint128_t is 2 parts, and is an object, the computer will be comparing a uint64_t to a class, not to a value. how do i force the computer to use the second == operator?
You shouldn’t even need the extra overloads for anything other than the conversion constructors. Then you just write the operators that handle uint128_t, and if you try to pass another convertible type, such as an int, it will be converted using the conversion constructor, then the appropriate operator will be called.
See, this works:
http://ideone.com/BEq03