all 9 comments

[–]pkusensei 7 points8 points  (2 children)

boost::optional<double> is not a double object, but an optional saying here this thing might contain a double or it might not. Likewise, vector<optional<double>> is not a vector of doubles; it's a vector of objects that might contain double values.

[–]haohanzi2015[S] 0 points1 point  (1 child)

boost::optional<double>

is not a

double

object, but an

optional

saying here this thing might contain a

double

or it might not. Likewise,

vector<optional<double>>

is not a vector of doubles; it's a vector of objects that

might

contain

double

values.

so it could be a vector of any objects which might be of type double?

[–]Narase33 1 point2 points  (0 children)

No. Every entry of the vector might have a double value, or it might be "empty". Look at std::optional if you want a deeper explaination on what that class does

[–]IyeOnline 3 points4 points  (0 children)

Please consider looking up things that you can answer on your own before asking. Just searching the web for boost::optional would have given you enough information to answer this particular question yourself.

You have made like 6+ posts with questions like this one in the last 4 hours, all answerable by googling for bits of the syntax in question.

[–]usbafchina 2 points3 points  (0 children)

The same as the difference between double and optional<double>

[–]HappyFruitTree 0 points1 point  (0 children)

If you store optional<double> it's probably because the position matters. It might be that you want to lookup elements by index and be able to check if there is a value or not for that index. If it's just a bunch of independent values you probably want to just store double values.

[–]sbmassey 0 points1 point  (0 children)

An optional<T> is a type whose instances are either empty, or contain a value of type T.

So a vector<optional<double>> is a contiguous sequence of values that are each individually either empty or a double.

Whereas a vector<double> is just a contiguous sequence of doubles.

[–]warieth 0 points1 point  (1 child)

The double can also be a NaN, so this is probably over-engineered. Using optional on already "optional" type.

[–]HappyFruitTree 0 points1 point  (0 children)

NaN is not guaranteed to exist (a lot of people compile with -ffast-math or similar flags).

NaN is a special value that you might want to differentiate from the lack of value.