This simple code can’t be compiled with the -std=c++20
option:
#include <limits>
#include <boost/multiprecision/float128.hpp>
namespace bm = boost::multiprecision;
int main()
{
auto const m = std::numeric_limits<bm::float128>::max();
}
The compilation command and its error output:
hekto@ubuntu:~$ g++ -std=c++20 test.cpp
In file included from test.cpp:2:
/usr/include/boost/multiprecision/float128.hpp: In instantiation of ‘static std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::number_type std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max() [with boost::multiprecision::expression_template_option ExpressionTemplates = boost::multiprecision::et_off; std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::number_type = boost::multiprecision::number<boost::multiprecision::backends::float128_backend, boost::multiprecision::et_off>]’:
test.cpp:8:53: required from here
/usr/include/boost/multiprecision/float128.hpp:728:55: error: could not convert ‘boost::multiprecision::quad_constants::quad_max’ from ‘const __float128’ to ‘std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, boost::multiprecision::et_off> >::number_type’ {aka ‘boost::multiprecision::number<boost::multiprecision::backends::float128_backend, boost::multiprecision::et_off>’}
728 | static number_type (max)() BOOST_NOEXCEPT { return BOOST_MP_QUAD_MAX; }
| ^~~~~~~~~~~~~~~~~
| |
| const __float128
Why is that?
- OS: Xubuntu 20.04.4 LTS
- Compiler: g++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
- Boost: 1.71.0
2
Answers
Directly from the documentation:
The same applies for
c++20
/gnu++20
, the docs merely haven’t yet been updated for that revision.The documentation states:
So you need to specify
--std=gnu++20
instead of--std=c++20
. The boost documentation is not up-to-date. The flag enables various GNU extensions, one of it being__float128
.See example on godbolt.
The underlying reason for the compiler error is that without the
--std=gnu++17
or--std=gnu++20
flag,_GLIBCXX_USE_FLOAT128
is not defined, meaningBOOST_HAS_FLOAT128
is not defined, meaning__float128
is not recognized asnumber_kind_floating_point
(but asnumber_kind_unknown
).This means that
boost::multiprecision::number
cannot be implicitly constructed from a__float128
becauseis_restricted_conversion<__float128, boost::multiprecision::float128_backend>::value
istrue
becausebm::detail::is_lossy_conversion<__float128, boost::multiprecision::float128_backend>::value
istrue
, because__float128
isnumber_kind_unknown
instead ofnumber_kind_floating_point
.I.e. in short, without
gnu++20
, theboost::multiprecision::float128
type is not supported.