gpt4 book ai didi

c++ - 候选函数和声明顺序

转载 作者:行者123 更新时间:2023-12-04 14:54:43 26 4
gpt4 key购买 nike

考虑以下片段:

template<typename T>
int foo(T) {
return 1;
}

struct my_struct{};

template<typename T>
int do_foo(T t) {
return
foo(my_struct{}) + // 1
foo(t); // 2 (via ADL)
}

int foo(my_struct) {
return 2;
}

int main () {
return do_foo(my_struct{});
}

乍一看,对 foo 的“非依赖”调用将返回 1,而“依赖”调用将返回 2 .

然而,在标准中我们发现the following paragraph :

If the call would <...> find a better match had the lookup within theassociated namespaces considered all the function declarations withexternal linkage introduced in those namespaces in all translationunits, not just considering those declarations found in the templatedefinition and template instantiation contexts, then the program hasundefined behavior.

目前还不清楚这段是否只讨论从属名称查找 - 所以我想知道它是否适用于上面的代码,从而使其格式错误。

你能举一个例子来说明这一段的意思吗?

最佳答案

这整段仅适用于使用 ADL 解析的函数:

For a function call where the postfix-expression is a dependent name [...]
If the call would [...]

(这里的“调用”指的是每个调用都有一个从属名称,因此这不适用于 foo(my_struct{}) ,它不依赖)

第二种情况由 one definition rule 涵盖:

There can be more than one definition of a [...] non-static function template [...] in a program provided that each definition appears in a different translation unit, and the provided definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

  • [...]
  • in each definition of D, corresponding names, looked up according to [basic.lookup], shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution and after matching of partial template specialization ([temp.over]), [...]

您的代码应该定义良好,只要在其他翻译单元中没有为 int foo(my_struct) 定义出现在 template<typename T> int do_foo(T t) 的定义之前.


对于 [temp.dep.candidate] 的示例,以下两个翻译单元将具有未定义的行为:

template<typename T>
int foo(T) {
return 1;
}

struct my_struct {};

template<typename T>
int do_foo(T t) {
return foo(t);
}

int main() {
return do_foo(my_struct{});
}
struct my_struct {};

int foo(my_struct) {
return 2;
}

(因为 int foo(my_struct) 会更好,但在模板实例化上下文中找不到)

关于c++ - 候选函数和声明顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68305792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com