<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++ 模板

        时间:2023-09-26

        • <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++ 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 void* 到基类指针的转换 下一篇:&amp;(*similarObject) 和similarObject 之间的区别?他们不一样吗?

                  相关文章

                • <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>

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