ned14 / outcome

Provides very lightweight outcome<T> and result<T> (non-Boost edition)
https://ned14.github.io/outcome
Other
676 stars 62 forks source link

How to use [[likely]] for BOOST_OUTCOME_TRY_LIKELY #237

Closed Tradias closed 3 years ago

Tradias commented 3 years ago

Since BOOST_OUTCOME_TRY_LIKELY is used within parentheses in if(BOOST_OUTCOME_TRY_LIKELY(...)) it is not straight forward to define it in such a way that it makes use of the C++20 [[likely]] attribute. The following is possible:

BOOST_OUTCOME_TRY_LIKELY(expr) [&] {if ((expr)) [[likely]] {return true;}return false;}()

and produces the correct improvement for GCC: https://godbolt.org/z/Yf76je but makes codegen for MSVC worse: https://godbolt.org/z/ovx7sb. Additionally most compilers have not implemented support for capturing structured bindings in lambdas yet which makes it even less ideal.

ned14 commented 3 years ago

I am afraid I do not understand the concern raised here. OUTCOME_TRY already tells the compiler that success is likely. OUTCOME_TRY_FAILURE_LIKELY tells the compiler that failure is likely.

Those macros do not current use C++ 20 attributes, they use the proprietary compiler builtins, but it's the same difference.

Are you asking instead that if on C++ 20, that those macros should only use the C++ 20 attributes?

Tradias commented 3 years ago

Yes, I think in C++20 mode it makes sense to use the attributes.

The code for the macro BOOST_OUTCOME_TRY_LIKELY makes it look as if the user is allowed to provide its own definition of it, which is why I started fiddling with it. Checking the documentation again, I release that it is not part of the public API and I therefore understand that it is not meant to be defined in the way I described above.

ned14 commented 3 years ago

Fixed, and thanks for the proposal!