Boost logo

Boost Users :

Subject: [Boost-users] [boost/config] BOOST_LIKELY argument requirements
From: Stephan Bergmann (sbergman_at_[hidden])
Date: 2014-09-25 03:15:16


Looking at the definition of BOOST_LIKELY as

   #define BOOST_LIKELY(x) __builtin_expect(x, 1)

in boost/config/compiler/{clang,intel,gcc}.hpp, and given that at lest
the GCC and Clang __builtin_expect has a first parameter of type long, I
wonder

(1) whether it would be better to define it as

   #define BOOST_LIKELY(x) __builtin_expect(bool(x), 1)

so that

> $ cat test.cc
> #include "boost/config.hpp"
> #include "boost/logic/tribool.hpp"
> int f(boost::tribool b1, boost::tribool b2) {
> if (b1) return 1;
> else if (BOOST_LIKELY(b2)) return 2;
> else return 3;
> }
>
> $ g++ -c test.cc
> In file included from .../boost_1_56_0/boost/config.hpp:39:0,
> from test.cc:1:
> test.cc: In function ‘int f(boost::logic::tribool, boost::logic::tribool)’:
> .../boost_1_56_0/boost/config/compiler/gcc.hpp:90:46: error: cannot convert ‘boost::logic::tribool’ to ‘long int’ for argument ‘1’ to ‘long int __builtin_expect(long int, long int)’
> #define BOOST_LIKELY(x) __builtin_expect(x, 1)
> ^
> test.cc:5:14: note: in expansion of macro ‘BOOST_LIKELY’
> else if (BOOST_LIKELY(b2)) return 2;
> ^

would not fail (effectively changing the requirement for the argument to
BOOST_LIKELY from "[the] expression should result in a boolean value" to
"it should be contextually convertible to bool").

(2) if not that, whether to at least wrap the expansion of x in parentheses,

   #define BOOST_LIKELY(x) __builtin_expect((x), 1)

Stephan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net