Form academic point of view, if I wanted to return std::nullopt as a non-constant reference. How will I be doing the same.
Little background, today when I was working on a code returning an std::optional> reference but I forgot to make the return constant and I got the error.
Error (active) E0434 a reference of type "std::optional<std::vector<std::any, std::allocator<std::any>>> &" (not const-qualified) cannot be initialized with a value of type "const std::nullopt_t" Socket.IO D:\Hardware\Windows\Visual Studio\Socket.IO\Socket.IO\main.cpp 46 Error C2440 'return': cannot convert from 'const std::nullopt_t' to 'std::optional<std::vector<std::any,std::allocator<_Ty>>> &' Socket.IO d:\hardware\windows\visual studio\socket.io\socket.io\main.cpp 46
Just wanted to know if someone wanted to return an non-constant reference using std::optional how would he be doing so.
Platform Used : Windows 10 Pro x64
Development Environment : Visual Studios 15.9.9
std::vector<int>> a;
std::optional<std::vector<int>>& test(int b)
{
a.clear();
a.push_back(b);
if(b)
return a;
else
return std::nullopt;
}