boostorg / core

Boost Core Utilities
133 stars 83 forks source link

boost::core::bit_width not constexpr in VS #109

Closed joaquintides closed 1 year ago

joaquintides commented 2 years ago

The following

#include <boost/core/bit.hpp>

int x[boost::core::bit_width(16u)];

yields

<source>(3): error C3615: constexpr function 'boost::core::bit_width' cannot result in a constant expression
C:/data/libraries/installed/x64-windows/include\boost/core/bit.hpp(445): note: failure was caused by call of undefined function or one not declared 'constexpr'
C:/data/libraries/installed/x64-windows/include\boost/core/bit.hpp(445): note: see usage of 'boost::core::countl_zero'
ASM generation compiler returned: 2
pdimov commented 2 years ago

Yes, because the MSVC bitscan intrinsics aren't constexpr.

joaquintides commented 2 years ago

Can't std::is_constant_evaluated() be leveraged to remedy this?

pdimov commented 2 years ago

std::is_constant_evaluated is C++20, so the gains will be marginal.

joaquintides commented 2 years ago

This is better than nothing... At least the BOOST_CONSTEXPR qualifier should be conditionally removed in VS. Another possibility is to provide constexpr_bit_width etc. versions for those who need them.

pdimov commented 2 years ago

There's no need to remove the qualifier.

It might be possible to use the is_constant_evaluated intrinsic: https://github.com/microsoft/STL/blob/5e3574b97985db31112a1c60e4eb52e821d25a83/stl/inc/xtr1common#L170

This should be available in all language modes, but I'll need to check which MSVC versions have it.

joaquintides commented 2 years ago

Looks like the intrinsic is available from Visual Studio 2019, versión 16.5 onwards.