value checking function

Is there a function in standard C++ that would determine wheather a variable has value or not? I mean something like

1
2
3
4
int a;
somefunction(a); // false
int b = 10;
somefunction(b); // true 


Thanks.
no.
Any advise on how can I create it?
pizet wrote:
Is there a function in standard C++ that would determine wheather a variable has value or not?


When you say no value, are you referring to an uninitialized variable or one set to NULL? An uninitialized variable is going to have some garbage value and attempting to do something with it will probably result in a run time error.
A variable always has some value, but if it's not initialized you can't tell what value it is. So you can't check it. As a rule, always initialize your variables.
Create a wrapper class with a boolean member that tells you if the value is valid or not.
Last edited on
the boost library provides boost::optional. That would be:

1
2
3
4
5
boost::optional<int> a;
if(a)
  do_something(*a); // note: * to get the value
else
  no_value();
Topic archived. No new replies allowed.