I have a function declared as:
void foo(unsigned int x)
How can I check that foo() is not receiving negative numbers? I'd like that if I call foo(-1) an exception is thrown, but of course since x is converted automatically to unsigned, I can't check for its positivity like:
if not(x>=0){ do_something();};
or similar checks.
There is not really any good way to do it, but you can do it with some tricks:
1) declare an undefined better match:
void foo(unsigned int x) {
//do something
}
void foo(int x);
2) use typeid to determine the type.
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