1. <tfoot id='uOJf9'></tfoot>
      <bdo id='uOJf9'></bdo><ul id='uOJf9'></ul>

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

    1. <small id='uOJf9'></small><noframes id='uOJf9'>

      <legend id='uOJf9'><style id='uOJf9'><dir id='uOJf9'><q id='uOJf9'></q></dir></style></legend>

      获取模板化函数的地址是否应该触发其编译?

      时间:2023-10-17
      <legend id='FgF4h'><style id='FgF4h'><dir id='FgF4h'><q id='FgF4h'></q></dir></style></legend>
      • <small id='FgF4h'></small><noframes id='FgF4h'>

        • <bdo id='FgF4h'></bdo><ul id='FgF4h'></ul>

              <tbody id='FgF4h'></tbody>
            1. <tfoot id='FgF4h'></tfoot>

              <i id='FgF4h'><tr id='FgF4h'><dt id='FgF4h'><q id='FgF4h'><span id='FgF4h'><b id='FgF4h'><form id='FgF4h'><ins id='FgF4h'></ins><ul id='FgF4h'></ul><sub id='FgF4h'></sub></form><legend id='FgF4h'></legend><bdo id='FgF4h'><pre id='FgF4h'><center id='FgF4h'></center></pre></bdo></b><th id='FgF4h'></th></span></q></dt></tr></i><div id='FgF4h'><tfoot id='FgF4h'></tfoot><dl id='FgF4h'><fieldset id='FgF4h'></fieldset></dl></div>
                本文介绍了获取模板化函数的地址是否应该触发其编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我得到了这个问题的官方回答,decltype 应该触发函数编译.事实上,decltype 对已声明但未定义的函数是合法的.

                I got an official answer to this question that decltype should not trigger function compilation. In fact decltype on a function that is declared but not defined is legal.

                下一个问题,取函数的地址是否应该触发函数的编译?以这个例子:

                Next question, should taking the address of a function trigger the compilation of a function? Take this example:

                template <typename T>
                void foo(T&& x) { x.func(); }
                
                int main()
                {
                    auto bar = &foo<int>;
                }
                

                我测试过的所有编译器都失败并显示如下错误:

                All the compilers I've tested fail with an error like:

                请求x中的成员func,属于非类类型int

                Request for member func in x, which is of non-class type int

                但如果我只是定义 foo 而没有声明它,代码编译得很好.有人可以向我提供有关获取函数地址是否需要编译的官方来源吗?

                But if I just define foo and don't declare it, the code compiles fine. Can someone provide me with an official source on whether taking the address of a function should require it's compilation?

                推荐答案

                3.2/2:

                一个表达式可能会被求值,除非它是一个未求值的操作数(第 5 条)或其子表达式.... 一个非重载其名称显示为潜在求值表达式或一组候选函数的成员,如果被重载选中从潜在评估表达式中引用时的分辨率,被 odr 使用,除非它是一个纯虚函数并且它的名字不是明确限定.

                An expression is potentially evaluated unless it is an unevaluated operand (Clause 5) or a subexpression thereof. ... A non-overloaded function whose name appears as a potentially-evaluated expression or a member of a set of candidate functions, if selected by overload resolution when referred to from a potentially-evaluated expression, is odr-used, unless it is a pure virtual function and its name is not explicitly qualified.

                然后是 3.2/3:

                每个程序都应该包含每个非内联程序的一个定义在该程序中 odr 使用的函数或变量;没有诊断必需的.定义可以显式地出现在程序中,它可以可以在标准或用户定义的库中找到,或者(当适当)它是隐式定义的(见 12.1、12.4 和12.8).内联函数应在每个使用 odr 的翻译单元中定义.

                Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is odr-used.

                函数名绝对不是未计算的操作数(例如 sizeof, decltype),并且它出现在表达式中,因此它可能会被计算.然后第二个需要在每个翻译单元中恰好有一个非内联定义,或相同的内联定义.

                The function name is definitely not an unevaluated operand (for example to sizeof, decltype), AND it appears in an expression, so it's potentially evaluated. Then the second one requires exactly one non-inline definition, or identical inline definitions in each translation unit.

                这篇关于获取模板化函数的地址是否应该触发其编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Visual Studio IDE (2008) 中打开多 CPU/核心 C++ 编译? 下一篇:是否允许 C++ 编译器发出不同的机器代码来编译相同的程序?

                相关文章

                • <bdo id='VLNpx'></bdo><ul id='VLNpx'></ul>

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

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