<legend id='KoQcu'><style id='KoQcu'><dir id='KoQcu'><q id='KoQcu'></q></dir></style></legend>
    1. <small id='KoQcu'></small><noframes id='KoQcu'>

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

      我应该在 C 样式转换上使用 C++ reinterpret_cast 吗?

      Should I use a C++ reinterpret_cast over a C-style cast?(我应该在 C 样式转换上使用 C++ reinterpret_cast 吗?)
        <legend id='V31oV'><style id='V31oV'><dir id='V31oV'><q id='V31oV'></q></dir></style></legend>
          <bdo id='V31oV'></bdo><ul id='V31oV'></ul>

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

            1. <tfoot id='V31oV'></tfoot>
                <tbody id='V31oV'></tbody>
              • <i id='V31oV'><tr id='V31oV'><dt id='V31oV'><q id='V31oV'><span id='V31oV'><b id='V31oV'><form id='V31oV'><ins id='V31oV'></ins><ul id='V31oV'></ul><sub id='V31oV'></sub></form><legend id='V31oV'></legend><bdo id='V31oV'><pre id='V31oV'><center id='V31oV'></center></pre></bdo></b><th id='V31oV'></th></span></q></dt></tr></i><div id='V31oV'><tfoot id='V31oV'></tfoot><dl id='V31oV'><fieldset id='V31oV'></fieldset></dl></div>
                本文介绍了我应该在 C 样式转换上使用 C++ reinterpret_cast 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下模板函数用于将任何标准类型的数据转储到二进制输出流中.

                I have the following template function used to dump data of any standard type into a binary output stream.

                template<typename T> static void
                dump ( const T& v, ostream& o ) {
                    o.write ( reinterpret_cast<const char*>(&v), sizeof(T));
                }
                

                除了 reinterpret_cast,我还可以使用 C 风格(const char*).使用 reinterpret_cast 有什么特别的理由吗?我阅读了其他一些帖子,其中 reinterpret_cast 不受欢迎.但是上面的用法是合法的,不能用别的代替吧?

                Instead of the reinterpret_cast I could also use a C-style (const char*). Is there any particular reason to use reinterpret_cast? I read a few other posts where reinterpret_cast was frowned upon. But the above usage is legal and cannot be replaced with anything else, right?

                推荐答案

                C 风格强制转换的问题在于它们在幕后做了很多事情.详细解释见这里:http://anteru.net/2007/12/18/200/

                The problem with C-Style casts is that they do a lot under the hood. See here for a detailed explanation: http://anteru.net/2007/12/18/200/

                您应该尝试始终使用 C++-casts,从长远来看会让生活更轻松.在这种情况下,C 样式转换的主要问题是您可以编写 (char*)(&v) 而使用 reinterpret_cast,您需要额外的 const_cast,所以更安全一些.此外,您可以使用正则表达式轻松找到 reinterpret_cast,而这对于 C 风格的转换是不可能的.

                You should try to always use the C++-casts, makes life easier in the long run. The main problem with C-style casts in this case is that you could have written (char*)(&v) while with reinterpret_cast, you would need an additional const_cast, so it's a bit safer. Plus you can easily find reinterpret_cast with a regex, which is not possible for the C-style casts.

                这篇关于我应该在 C 样式转换上使用 C++ reinterpret_cast 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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(使用非平凡的构造函数初始化联合)

                      <tbody id='J1GQ5'></tbody>

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

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