The reasoning of std::optional
is made by saying that it may or may not contain a value. Hence, it saves us the effort of constructing a, probably, big object, if we don't need it.
For example, a factory here, will not construt the object if some condition is not met:
#include <string>
#include <iostream>
#include <optional>
std::optional<std::string> create(bool b)
{
if(b)
return "Godzilla"; //string is constructed
else
return {}; //no construction of the string required
}
But then how is this different from this:
std::shared_ptr<std::string> create(bool b)
{
if(b)
return std::make_shared<std::string>("Godzilla"); //string is constructed
else
return nullptr; //no construction of the string required
}
What is it that we win by adding std::optional
over just using std::shared_ptr
in general?
What is it that we win by adding std::optional over just using std::shared_ptr in general?
Let's say you need to return a symbol from a function with flag "not a value". If you would use std::shared_ptr
for that you would have huge overhead - char
would be allocated in dynamic memory, plus std::shared_ptr
would maintain control block. While std::optional on another side:
If an optional contains a value, the value is guaranteed to be allocated as part of the optional object footprint, i.e. no dynamic memory allocation ever takes place. Thus, an optional object models an object, not a pointer, even though the operator*() and operator->() are defined.
so no dynamic memory allocation is involved and difference comparing even to the raw pointer could be significant.