Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to check that a function is receiving an unsigned int?

Tags:

c++

int

unsigned

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.

like image 468
lucacerone Avatar asked Sep 16 '12 01:09

lucacerone


1 Answers

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.

like image 73
wich Avatar answered Oct 31 '22 14:10

wich