使用不同的行为提升uint128_t

时间:2016-04-28 21:34:39

标签: c++ boost g++ clang++

我有以下程序:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;

int main() {
    boost::multiprecision::uint128_t res;
    inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &res);
    cout<<res<<endl;
}

根据我使用的编译器,我有3种不同的行为:

铛++ - 3.8

23926103924128485722661168748298043680

克++ - 5.3.0

23926103924128485722661168748298043680

铛++ - 3.6

3087860000

克++ - 6.1.0

In file included from /usr/local/include/boost/config.hpp:61:0,
             from /usr/local/include/boost/cstdint.hpp:36,
             from /usr/local/include/boost/multiprecision/cpp_int.hpp:11,
             from main.cpp:4:
/usr/local/include/boost/multiprecision/cpp_int.hpp:181:4: error: right operand of shift expression '(1u << 63u)' is >= than the precision of the left operand [-fpermissive]
BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = 1u << (limb_bits - 1));
^
/usr/local/include/boost/multiprecision/cpp_int.hpp:404:4: error: right operand of shift expression '(1u << 63u)' is >= than the precision of the left operand [-fpermissive]
BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = 1u << (limb_bits - 1));
^
/usr/local/include/boost/multiprecision/cpp_int.hpp:556:4: error: right operand of shift expression '(1u << 63u)' is >= than the precision of the left operand [-fpermissive]
BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = 1u << (limb_bits - 1));
^
在所有情况下都启用了

c++14。有人可以解释为什么会这样吗?当然,预期的行为是第一个。

1 个答案:

答案 0 :(得分:1)

inet_pton采用无效*

您正在传递非原始地址和未记录的布局。

结果是最好的实现定义。可能是未定义的行为。