martinmoene / expected-dark

Expected objects for C++11 and later (and later perhaps C++98 )
MIT License
52 stars 4 forks source link

expected<void> + return #18

Open cdemez opened 6 years ago

cdemez commented 6 years ago

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

        // Here some processing  

}

Any idea to solve this case ?

cdemez commented 6 years ago

BTW,

Adding such code to expected.hpp helps:

template inline auto make_expected() -> expected<void, E> { return expected<void, E>(nsel_in_place); }

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)

martinmoene commented 6 years ago

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();
}
martinmoene commented 6 years ago

I have to revisit expected lite to see if make_expected() for the void specialization is (indeed) missing.

martinmoene commented 6 years ago

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.

cdemez commented 6 years ago

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 !

martinmoene commented 6 years ago

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] } ^

martinmoene commented 6 years ago

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.

cdemez commented 6 years ago

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 ;-)

cdemez commented 6 years ago

BTW, thanks and great work ;-)