<legend id='DSyVO'><style id='DSyVO'><dir id='DSyVO'><q id='DSyVO'></q></dir></style></legend>
    <i id='DSyVO'><tr id='DSyVO'><dt id='DSyVO'><q id='DSyVO'><span id='DSyVO'><b id='DSyVO'><form id='DSyVO'><ins id='DSyVO'></ins><ul id='DSyVO'></ul><sub id='DSyVO'></sub></form><legend id='DSyVO'></legend><bdo id='DSyVO'><pre id='DSyVO'><center id='DSyVO'></center></pre></bdo></b><th id='DSyVO'></th></span></q></dt></tr></i><div id='DSyVO'><tfoot id='DSyVO'></tfoot><dl id='DSyVO'><fieldset id='DSyVO'></fieldset></dl></div>

      1. <tfoot id='DSyVO'></tfoot>
        • <bdo id='DSyVO'></bdo><ul id='DSyVO'></ul>

        <small id='DSyVO'></small><noframes id='DSyVO'>

        用于安全整数转换的 C++ 模板

        C++ Template for safe integer casts(用于安全整数转换的 C++ 模板)

        • <small id='BEAPs'></small><noframes id='BEAPs'>

          1. <tfoot id='BEAPs'></tfoot>

            1. <legend id='BEAPs'><style id='BEAPs'><dir id='BEAPs'><q id='BEAPs'></q></dir></style></legend>
              <i id='BEAPs'><tr id='BEAPs'><dt id='BEAPs'><q id='BEAPs'><span id='BEAPs'><b id='BEAPs'><form id='BEAPs'><ins id='BEAPs'></ins><ul id='BEAPs'></ul><sub id='BEAPs'></sub></form><legend id='BEAPs'></legend><bdo id='BEAPs'><pre id='BEAPs'><center id='BEAPs'></center></pre></bdo></b><th id='BEAPs'></th></span></q></dt></tr></i><div id='BEAPs'><tfoot id='BEAPs'></tfoot><dl id='BEAPs'><fieldset id='BEAPs'></fieldset></dl></div>

                  <tbody id='BEAPs'></tbody>
                  <bdo id='BEAPs'></bdo><ul id='BEAPs'></ul>
                  本文介绍了用于安全整数转换的 C++ 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个 C++ 模板函数,它将在整数溢出时引发运行时异常,该函数在不同整数类型、不同宽度和可能的有符号/无符号不匹配之间进行转换.出于这些目的,我不关心从浮点类型到整数类型的转换,也不关心其他对象到对象的转换.我想这样做而不必编写大量特殊情况代码.这是我目前拥有的:

                  I am trying to write a C++ template function that will throw a runtime exception on integer overflow in casts between different integral types, with different widths, and possible signed/unsigned mismatch. For these purposes I'm not concerned with casting from floating-point types to integral types, nor other object-to-object conversions. I'd like to do this without having to write lots of special case code. This is what I currently have:

                  template< typename T, typename R > void safe_cast( const T& source, R& result )
                  {
                      // get the maximum safe value of type R
                      R rMax = (R) ~0;
                      if ( rMax < 0 ) // R is a signed type
                      {
                          // assume that we're on an 8-bit twos-compliment machine
                          rMax = ~( 0x80 << ( ( sizeof( R ) - 1 ) * 8 ) );
                      }
                  
                      if ( ( source & rMax  ) != source )
                      {
                          throw new IntegerOverflowException( source );
                      }
                  
                      result = static_cast<R>( source );
                  }
                  

                  这是否正确且有效?

                  由于各种原因 stl 不可用,所以我不能使用 std::numeric_limits,而 Boost 中的任何东西都是正确的.

                  For various reasons stl isn't available, so I can't use std::numeric_limits, and anything from Boost is right out.

                  推荐答案

                  您是否尝试过 SafeInt?它是一个跨平台模板,将对各种整数类型进行整数溢出检查.github上已经有

                  Have you tried SafeInt? It's a cross platform template that will do integer overflow checks for a variety of integer types. It's available on github

                  • https://github.com/dcleblanc/SafeInt

                  这篇关于用于安全整数转换的 C++ 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                • <tfoot id='ofIJP'></tfoot>

                  1. <legend id='ofIJP'><style id='ofIJP'><dir id='ofIJP'><q id='ofIJP'></q></dir></style></legend>
                  2. <i id='ofIJP'><tr id='ofIJP'><dt id='ofIJP'><q id='ofIJP'><span id='ofIJP'><b id='ofIJP'><form id='ofIJP'><ins id='ofIJP'></ins><ul id='ofIJP'></ul><sub id='ofIJP'></sub></form><legend id='ofIJP'></legend><bdo id='ofIJP'><pre id='ofIJP'><center id='ofIJP'></center></pre></bdo></b><th id='ofIJP'></th></span></q></dt></tr></i><div id='ofIJP'><tfoot id='ofIJP'></tfoot><dl id='ofIJP'><fieldset id='ofIJP'></fieldset></dl></div>
                    • <bdo id='ofIJP'></bdo><ul id='ofIJP'></ul>
                        <tbody id='ofIJP'></tbody>

                        <small id='ofIJP'></small><noframes id='ofIJP'>