<small id='3tZFS'></small><noframes id='3tZFS'>

    1. <legend id='3tZFS'><style id='3tZFS'><dir id='3tZFS'><q id='3tZFS'></q></dir></style></legend>
      <tfoot id='3tZFS'></tfoot>

        <bdo id='3tZFS'></bdo><ul id='3tZFS'></ul>

        <i id='3tZFS'><tr id='3tZFS'><dt id='3tZFS'><q id='3tZFS'><span id='3tZFS'><b id='3tZFS'><form id='3tZFS'><ins id='3tZFS'></ins><ul id='3tZFS'></ul><sub id='3tZFS'></sub></form><legend id='3tZFS'></legend><bdo id='3tZFS'><pre id='3tZFS'><center id='3tZFS'></center></pre></bdo></b><th id='3tZFS'></th></span></q></dt></tr></i><div id='3tZFS'><tfoot id='3tZFS'></tfoot><dl id='3tZFS'><fieldset id='3tZFS'></fieldset></dl></div>
      1. 为什么两个函数的地址相同?

        时间:2023-05-25
        • <bdo id='RLyW0'></bdo><ul id='RLyW0'></ul>
          1. <legend id='RLyW0'><style id='RLyW0'><dir id='RLyW0'><q id='RLyW0'></q></dir></style></legend>

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

              <tfoot id='RLyW0'></tfoot>
                    <tbody id='RLyW0'></tbody>

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

                  本文介绍了为什么两个函数的地址相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  考虑这个函数模板:

                  template<typename T>
                  unsigned long f(void *) { return 0;}
                  

                  现在,我将 ff 的地址打印为:

                  Now, I print the addresses of f<A> and f<B> as:

                  std::cout << (void*)f<A> << std::endl;
                  std::cout << (void*)f<B> << std::endl;
                  

                  如果在 MSVS10 中编译,为什么它们打印相同的地址?它们不是两个不同的功能,因此应该打印不同的地址吗?

                  Why do they print the same address if compiled in MSVS10? Are they not two different functions and therefore should print different addresses?

                  更新:

                  我意识到在 ideone 上,它会打印不同的地址.MSVS10 优化了代码,因为该函数不以任何方式依赖 T,因此它产生相同的函数.@Mark 对此的回答和评论很有价值.:-)

                  I realized that on ideone, it prints the different address. MSVS10 optimizes the code, as the function doesn't depend on T in any way, so it produces same function. @Mark's answer and comments on this are valuable. :-)

                  推荐答案

                  由于函数不依赖模板参数,编译器可以将所有实例化为一个函数.

                  Since the function doesn't depend on the template parameter, the compiler can condense all instantiations into a single function.

                  我不知道你为什么得到 1 作为地址.

                  I don't know why you get 1 for the address.

                  我用我的真实代码进行了试验,并得出结论,@Mark 上面所说的在这里非常重要:

                  I experimented with my real code, and concluded that what @Mark said above is very important here :

                  由于函数不依赖于模板参数,编译器可以将所有实例化为一个函数.

                  我还得出一个结论,如果函数体依赖于T*,而不是T,它仍然为我的不同类型参数生成相同的函数真正的代码(虽然不是在 ideone 上).然而,如果它依赖于 T,那么它会产生不同的函数,因为 sizeof(T) 对于不同的类型参数是不同的(对我来说很幸运).

                  I also came to a conclusion that if the function-body depends on T*, not on T, it still produces the same function for different type arguments in my real code (not on ideone, though). However, if it depends on T, then it produces different functions, because sizeof(T) differs (fortunately for me) for different type arguments.

                  所以我在函数模板中添加了一个 T 类型的虚拟 automatic 变量,这样函数就可以依赖于 T 的大小从而强制它产生不同的功能.

                  So I added a dummy automatic variable of type T in the function template, so that the function could depend on the size of T so as to force it to produce different functions.

                  这篇关于为什么两个函数的地址相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                        <tfoot id='tgDcP'></tfoot>
                      2. <legend id='tgDcP'><style id='tgDcP'><dir id='tgDcP'><q id='tgDcP'></q></dir></style></legend>

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