I recently discovered std::optional
as a way to improve the clarity of my code, especially for return value of functions. However I had questions about its impact on performance. More specifically I wanted to know if it was possible to write a code similar to the one below that would allow the compiler to apply Named Return Value Optimization.
struct Data
{
int x;
int y;
};
std::optional<Data> makeData(bool condition)
{
Data data;
if(condition)
{
data.x = 2.0;
data.y = 2.0;
return data;
}
else
{
return {};
}
}