应该采用模板化函数的地址触发其编译吗? [英] Should Taking the Address of a Templatized Function Trigger its Compilation?

查看:178
本文介绍了应该采用模板化函数的地址触发其编译吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于此问题的正式答案, 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:


请求会员 func x ,这是非类类型 int

但如果我只是define 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:是否可以提供函数的地址是否需要编译?

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:


每个程序都应包含每个非内联
函数的一个定义,在该程序中使用的变量;无需诊断
。定义可以在程序中显式出现,它可以在标准或用户定义的库中找到
,或者(当
适当时),它被隐式定义(见12.1,12.4和
12.8)。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆