c++ - 以不同的行为 boost uint128_t

标签 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 种不同的行为:

clang++-3.8

23926103924128485722661168748298043680

g++-5.3.0

23926103924128485722661168748298043680

clang++-3.6

3087860000

g++-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 在所有情况下均已启用。有人可以解释为什么会这样吗?预期的行为当然是第一个。

最佳答案

inet_pton 取一个 void*

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

结果充其量是实现定义的。可能是未定义的行为。

关于c++ - 以不同的行为 boost uint128_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36925881/

相关文章:

c++ - 使用 boost::asio 的 UDP 客户端和服务器设计

c++ - 使用结构作为参数 : Error: Struct not defined

c++ - g++:Ubuntu 发行版升级后,我的所有代码都不会链接

c++ - SFML 和代码:: block 0xc00000be

c++ - Boost.spirit (x3, boost 1.64) : how to implement this recursive rule correctly, 这可能吗?

C++ 包括使用 wxWidgets 和 Opengl 的依赖项

c++ - 混合 c 和 c++ 库(多 Unix 平台)

c++ - Boost序列化无法恢复保存的对象

c++ - boost::transform 与 std::transform

c++ - 编译 fatal error 头文件g++