简体   繁体   English

为什么uint128_t没有uint128_t的移位超载?

[英]Why doesn't uint128_t have bit shift overloads for uint128_t?

boost::multiprecision::uint128_t doesn't seem to have an overload for the left/right shift operator when both operands are uint128_t s: 当两个操作数均为uint128_t时, boost::multiprecision::uint128_t似乎对左/右移位运算符没有uint128_t

uint128_t operator<<(uint128_t, uint128_t); // Missing
uint128_t operator>>(uint128_t, uint128_t); // Also Missing

Some sample code: 一些示例代码:

#include <boost/multiprecision/cpp_int.hpp>

int main() {
    using uint128_t = boost::multiprecision::uint128_t;
    uint128_t number = 100;
    uint128_t ten = 10;

    auto leftShift = number << ten;  // fail
    auto rightShift = number >> ten; // fail
    return 0;
}

Here is a demo . 这是一个演示

The error message is pretty long, so here is the (modified) first message for the left shift operator: 错误消息很长,因此这是左移运算符的(修改后的)第一条消息:

prog.cpp:8:26: error: no match for 'operator<<' (operand types 
                      are 'uint128_t {aka boost_template_typedef_for_uint128_t}' 
                      and 'uint128_t {aka boost_template_typedef_for_uint128_t}')
  auto leftShift = number << ten;  // fail

You can look at the demo for the full error messages. 您可以在演示中查看完整的错误消息。

Why is that the case? 为什么会这样? I don't see a reason why they wouldn't be implemented, as unsigned a = 100u << 2u; 我看不出为什么不执行它们的原因,因为unsigned a = 100u << 2u; works perfectly fine for the same types. 对于相同的类型,效果很好。

It's because shifting so many bits to the left is rarely useful. 这是因为向左移动这么多位几乎没有用。 It kind-of defines floating point representations (use cpp_dec_float or similar). 它定义了浮点表示形式(使用cpp_dec_float或类似形式)。

Here's a workaround: 解决方法:

Live On Coliru 生活在Coliru

#include <boost/multiprecision/integer.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>

int main() {
    boost::multiprecision::uint128_t v("1"), u("20");

    v = v << u.convert_to<size_t>();
    std::cout << v;
}

Prints 打印

1048576

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM