I want to get a class to be flexible enough to handle explicit conversion based on the typename type in a template. How can it be done? I am guessing the class will have to handle the conversion
Note this may seem like a lot of code to slog through, but I kept it as short as I could.
Here is what I would like to do.
template<typename T> void fooAndBar(T x)
{
uint64_t count = 0;
//i want next line to work for x being either uint64_t or bigNumber
while( x <= (T)1000 ) {
count++;//do something
}
}
int main() {
uint64_t y = 1;
fooAndBar(y);
bigNumber z;
fooAndBar(z);
return 0;
}
with bigNumber defined as:
#include <vector>
typedef unsigned long long int uill;
class bigNumber //just a class that can handle reaaaly big numbers very striped down
{
public:
bigNumber();
~bigNumber() {}
bool operator<=(bigNumber rightSide);
template<typename T> static bigNumber convertToBigNumber(T in);
private:
std::vector<uill> digits;
};
bigNumber::bigNumber() { digits.push_back(1); }
bool bigNumber::operator<=(bigNumber rightSide) {
// dummy function
return digits.back() <= rightSide.digits.back();
}
template<typename T> static bigNumber bigNumber::convertToBigNumber(T in) {
bigNumber bn;
bn.digits.push_back(in);
return bn;
}
but I am stuck wring separate functions. One for uint64_t and one for bigNumber like so
void foo(uint64_t x) {
uint64_t count = 0;
// NB x is an uint64_t
while( x <= 1000 ) {
count++;//do something
}
}
void bar(bigNumber x) {
uint64_t count = 0;
// NB x is a bigNumber
while( x <= bigNumber::convertToBigNumber(1000) ) {
count++;//do something
}
}
int main() {
uint64_t y=1;
foo(y);
bigNumber z ;
bar(z);
return 0;
}
Thanks in advance for any help. And this question needs better tags any suggestions would help.
C++ can handle a implicit conversion from int to bigNumber. All you need to do is to add constructor for this case:
C++11:
bigNumber(uill e) : digits{e} {}
C++98:
bigNumber(uill e) { digits.push_back(e); }
and you will be good to go.