Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BOOST_UNLIKELY and boost::system::error_code #918

Open
ujos opened this issue Jun 4, 2024 · 1 comment
Open

BOOST_UNLIKELY and boost::system::error_code #918

ujos opened this issue Jun 4, 2024 · 1 comment

Comments

@ujos
Copy link

ujos commented Jun 4, 2024

Could you please modify the BOOST_LIKELY and BOOST_UNLIKELY macros in the following way:

#define BOOST_LIKELY(x)   (__builtin_expect(!!(x), 1))
#define BOOST_UNLIKELY(x) (__builtin_expect(!!(x), 0))
  1. I cannot easily use BOOST_UNLIKELY with boost::system::error_code. Compiler complains
error: cannot convert ‘boost::system::error_code’ to ‘long int’ for argument ‘1’ to ‘long int __builtin_expect(long int, long int)’
[build]      if (BOOST_UNLIKELY (ec)) {
[build]          ^ 

To workaround the problem I should use macro like following:

if (BOOST_UNLIKELY (!!ec))

... which looks weird and causes unwanted questions during the code review.

  1. I have to add extra braces:
if (BOOST_UNLIKELY (!!ec))
   ^ -- here             ^ --- and here

... instead of:

if BOOST_UNLIKELY (ec)
{
}
@Vasco0x4
Copy link

Vasco0x4 commented Jun 19, 2024

To modify the BOOST_LIKELY and BOOST_UNLIKELY macros to handle boost::system::error_code, you can redefine them to ensure compatibility without needing additional braces. Here's the updated version:

#define BOOST_UNLIKELY(x) (__builtin_expect(static_cast<bool>(x), 0))

This approach uses static_cast<bool>(x) to explicitly cast the condition to a boolean type, making it compatible with boost::system::error_code and similar types. This allows you to use the macros as follows:

    // Handle error
}

This change ensures the macros work correctly without needing extra braces.

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

No branches or pull requests

2 participants