Nullable values in C++

Boost.Optional probably does what you need.

boost::none takes the place of your CNullValue::Null(). Since it’s a value rather than a member function call, you can do using boost::none; if you like, for brevity. It has a conversion to bool instead of IsNull, and operator* instead of GetValue, so you’d do:

void writeToDB(boost::optional<int> optional_int) {
    if (optional_int) {
        pass *optional_int to database;
    } else {
        pass null to database;
    }
}

But what you’ve come up with is essentially the same design, I think.

Leave a Comment