Skip to content
This repository has been archived by the owner on Jun 3, 2018. It is now read-only.

expected<void> + return #18

Open
cdemez opened this issue Jan 4, 2018 · 9 comments
Open

expected<void> + return #18

cdemez opened this issue Jan 4, 2018 · 9 comments

Comments

@cdemez
Copy link

cdemez commented Jan 4, 2018

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
Copy link
Author

cdemez commented Jan 4, 2018

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
Copy link
Owner

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
Copy link
Owner

martinmoene commented Jan 4, 2018

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

@martinmoene
Copy link
Owner

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
Copy link
Author

cdemez commented Jan 5, 2018

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

cdemez commented Jan 5, 2018

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
Copy link
Author

cdemez commented Jan 5, 2018

BTW, thanks and great work ;-)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants