Например, фабрика здесь не будет создавать объект, если не выполнено какое-либо условие:
Код: Выделить всё
#include
#include
#include
std::optional create(bool b)
{
if(b)
return "Godzilla"; //string is constructed
else
return {}; //no construction of the string required
}
Код: Выделить всё
std::shared_ptr create(bool b)
{
if(b)
return std::make_shared("Godzilla"); //string is constructed
else
return nullptr; //no construction of the string required
}
Подробнее здесь: https://stackoverflow.com/questions/429 ... -stdunique