Open cdemez opened 6 years ago
BTW,
Adding such code to expected.hpp helps:
template
Then I do this:
return nonstd::make_expected
But it is not elegant... maybe you have a better solution ! I should just use "return" (Like I do at the end of the function)
Like so on Wandbox:
#include "expected.hpp"
nonstd::expected<void, int> initialize()
{
if ( true )
return nonstd::make_unexpected( 13 );
// Here some processing
return nonstd::expected<void, int>();
}
int main( int /*argc*/, char * /*argv*/[] )
{
auto result = initialize();
return result.error();
}
I have to revisit expected lite to see if make_expected()
for the void specialization is (indeed) missing.
Also note that make_expected()
is no longer part of the expected proposal, possibly because template parameter type deduction also works for constructors since C++17.
Thanks a lot Martin,
I see... but then 'maybe' you should remove the make_expected. BTW, it seems that also make_unexpected has been removed !
But, don't you think that is it not 'natural' (not easy to use) to return an empty expected ! We 'should' have an automatic type deduction !
Notice that at the end of the function we don't need to return anything, only with the 'return' keyword !
I think the language should not be changed to allow to not return an object for a non-void function(), just to accommodate a library type.
Notice that at the end of the function we don't need to return anything, only with the 'return' keyword !
That's likely because its unreachable code after the if (true)
...
Changing if (true)
to if (false)
yields:
prog.cc: In function 'nonstd::expected<void, int> initialize()': prog.cc:11:1: warning: control reaches end of non-void function [-Wreturn-type] } ^
For expected lite I may retain make_expected()
and make_unexpected()
as it is intended to be used with C++11 and later where there's no template parameter type deduction for constructors.
I understand your point, and agree ;-)
BTW, it will be great to have more examples, more uses cases.
On my side, I try to make it work with std::error_code... to have a more strict error reporting mecanism. I know that the type is optional, but still it is interesting to show such use case ;-)
BTW, thanks and great work ;-)
Hi, I tried to compile something like that, but I got a compilation error (MSVC2017):
"error C2561: 'initialize': function must return a value"
The code :
nonstd::expected<void, int> initialize() { if (true) return; // <-- Compilation error here
}
Any idea to solve this case ?