You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clang has recently addressed an issue where it failed to diagnose the UB of casting an out-of-range integer to an enum in a constant expression.
Since undefined behavior is not allowed in a constant expression, Clang now prohibits enum test { zero }; constexpr int g = static_cast<test>(-1); with "error: constexpr variable 'g' must be initialized by a constant expression; note: integer value -1 is outside the valid range of values [0, 1] for this enumeration type"
This change breaks boost::mpl::integral_c, since it does exactly that. Demonstrated with example, below (and on godbolt)
#include <boost/mpl/integral_c.hpp>
enum test { zero, one };
boost::mpl::integral_c<test, zero> m;
Originally discovered with a program that was using the boost::numeric library, which in turn uses mpl like the above example.
#include <boost/numeric/conversion/udt_builtin_mixture.hpp>
boost::numeric::udt_builtin_mixture<int, int> m;
Perhaps the correct fix is to somehow cause the next/prior members to be omitted when they're impossible to represent, just like they're omitted for bool? I expect that's possible somehow via some template specialization magic.
Note that Clang has been changed since a week ago to no longer have this be an unconditional error. Now it's a controllable warning -Wenum-constexpr-conversion, which by default is enabled as an error. However, it's suppressed in system headers, and can be downgraded/disabled in user code as well. So, to see the error in the godbolt example you'll now need to add -Wsystem-headers to the compiler options. (e.g. https://godbolt.org/z/v4PPjW1n1)
Since most code using boost will be using it as a system header, they'll also not see the error, and anyone else may downgrade the error to a warning or disable it entirely if desired. However, it may become an unconditional error again in some future release of Clang, so it'd still be good to address it.
Perhaps the correct fix is to somehow cause the next/prior members to be omitted when they're impossible to represent, just like they're omitted for bool? I expect that's possible somehow via some template specialization magic.
Well, that's what I investigated, and my (ill-informed) conclusion is that it's not possible. The problem is that the the only expressions that try to create the representation (static_cast<test>(-1)) are not constant expressions, and they're well-typed. I hope somebody proves me wrong, but I'm certainly out of ideas.
If the intent is that the enum should be able to handle values outside the enumerator values then perhaps making it have a fixed underlying type better represents the intent?
I don't think that's the intent, but it is a decent workaround for the problem. I don't believe the intent is actually realizable, but like I said, I no longer really know C++, so I might be wrong.
enum test { zero, one };
template<classE, E a>
E const k = static_cast<E>(a - 1);
template<classT, T> structS {};
template<class> intf();
intg() { return f<S<test, k<test, zero>>>(); }
Was there any progress here? Clang landed llvm/llvm-project#67528 which makes this an error by default even in system headers. We can turn off the warning for now, but that option will be removed in a future Clang release, so this will need to be addressed before then.
we are running into this issue now on MacPorts. current boost builds are failing with the now-default compiler, clang-16.
various patches are being suggested, versions of the ones listed in this PR, but if at all possible, we'd like to go with the one that will be finally integrated into boost.
Activity
dabrahams commentedon Aug 9, 2022
The fix looks like this.
Fix for Clang trunk if next or prior out of range
dabrahams commentedon Aug 10, 2022
Actually I'm back to believing there's no true fix if next and prior have to be of type
integral_c<T, ...>.jyknight commentedon Aug 16, 2022
Perhaps the correct fix is to somehow cause the next/prior members to be omitted when they're impossible to represent, just like they're omitted for bool? I expect that's possible somehow via some template specialization magic.
Note that Clang has been changed since a week ago to no longer have this be an unconditional error. Now it's a controllable warning
-Wenum-constexpr-conversion, which by default is enabled as an error. However, it's suppressed in system headers, and can be downgraded/disabled in user code as well. So, to see the error in the godbolt example you'll now need to add-Wsystem-headersto the compiler options. (e.g. https://godbolt.org/z/v4PPjW1n1)Since most code using boost will be using it as a system header, they'll also not see the error, and anyone else may downgrade the error to a warning or disable it entirely if desired. However, it may become an unconditional error again in some future release of Clang, so it'd still be good to address it.
dabrahams commentedon Aug 16, 2022
Well, that's what I investigated, and my (ill-informed) conclusion is that it's not possible. The problem is that the the only expressions that try to create the representation (
static_cast<test>(-1)) are not constant expressions, and they're well-typed. I hope somebody proves me wrong, but I'm certainly out of ideas.shafik commentedon Aug 23, 2022
If the intent is that the enum should be able to handle values outside the enumerator values then perhaps making it have a fixed underlying type better represents the intent?
dabrahams commentedon Aug 23, 2022
I don't think that's the intent, but it is a decent workaround for the problem. I don't believe the intent is actually realizable, but like I said, I no longer really know C++, so I might be wrong.
ecatmur commentedon Nov 16, 2022
the
static consttrick works: https://godbolt.org/z/KrhdzqYbxthis means that going all the way back to the EDG implementation/workaround actually "fixes" it: https://godbolt.org/z/8bTerz3WM
Change mpl::integral_c to boost::integral_constant to avoid Clang 16 …
Upgrade external/boost to latest Boost 1.81.0
https://github.com/boostorg/numeric_conversion/commit/50a1eae942effb0…
smeenai commentedon Oct 16, 2023
Was there any progress here? Clang landed llvm/llvm-project#67528 which makes this an error by default even in system headers. We can turn off the warning for now, but that option will be removed in a future Clang release, so this will need to be addressed before then.
carlosgalvezp commentedon Nov 24, 2023
Hi! Any progress? What is the ETA for fixing? Next Clang release in about 6 months will turn the warning into a non-suppressable hard error.
Use static const for next/prior from C++17
kencu commentedon Dec 20, 2023
we are running into this issue now on MacPorts. current boost builds are failing with the now-default compiler, clang-16.
various patches are being suggested, versions of the ones listed in this PR, but if at all possible, we'd like to go with the one that will be finally integrated into boost.
14 remaining items
Add test for issue boostorg#69
Use static const for next/prior from C++17
piersh-aetheros commentedon Jan 25, 2025
can this change be backported? many distros aren't shipping recent versions of boost yet. for example, RedHat 9 has Clang 18 and boost 1.75.0.
Egorundel commentedon Feb 26, 2025
Hello, guys!
Please, help me.
I don't understand what the problem is and how to fix it. Attached screenshots of errors.
I work in Qt Creator. It seems like I’m working with the GCC compiler, but for some reason I’m getting errors due to clang...
Here is my version of Boost in the screenshot below (1.74.0):
Ubuntu version: 22.04
boost: fix integral-wrapper header for use with Xcode 16
Change mpl::integral_c to boost::integral_constant to avoid Clang 16 …
fix(boost): updgrade boost for fixing boostorg/mpl#69
fix(boost): updgrade boost for fixing boostorg/mpl#69
fix(boost): updgrade boost for fixing boostorg/mpl#69
fix(boost): updgrade boost for fixing boostorg/mpl#69
fix(boost): updgrade boost for fixing boostorg/mpl#69
Change mpl::integral_c to boost::integral_constant to avoid Clang 16 …
fix(boost): updgrade boost for fixing boostorg/mpl#69
fix(boost): updgrade boost for fixing boostorg/mpl#69
fix(boost): updgrade boost for fixing boostorg/mpl#69