Check variable for value

Apr 6, 2011 at 1:54pm
I have declared a double without a value. Later on I give it the value of the solution of an equation. The equation might however not have a solution all the time, which would result in an empty variable. I need a way to check if the variable is empty or not so that my program knows if it should carry on or do something else. Any suggestions?
Apr 6, 2011 at 2:05pm
Variables always have a value, they cannot be empty.
For custom classes you can define a state in which they can be considered "empty" (such as a container having no elements), but types like double always have a value, even if you don't initialize them. In that case, their value in undefined.
Instead, you should have a bool variable that indicates whether there was a solution or not.
Apr 6, 2011 at 2:13pm
How would that work then?

1
2
3
bool check=false;
if("equation")
     check=true;


Or am I completely wrong?
Apr 6, 2011 at 2:20pm
Yeah, something like that works.
Apr 6, 2011 at 2:31pm
You can use boost::optional for that purpose: http://www.boost.org/doc/libs/1_46_1/libs/optional/doc/html/index.html
Topic archived. No new replies allowed.