候选函数和声明顺序
Function candidates and declaration order
考虑以下片段:
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
的“非依赖”调用将 return 1
,而“依赖”调用将 return 2
.
然而,在标准中我们发现 the following paragraph:
If the call would <...> find a better match had the lookup within the
associated namespaces considered all the function declarations with
external linkage introduced in those namespaces in all translation
units, not just considering those declarations found in the template
definition and template instantiation contexts, then the program has
undefined 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)
会更好,但在模板实例化上下文中找不到)
考虑以下片段:
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
的“非依赖”调用将 return 1
,而“依赖”调用将 return 2
.
然而,在标准中我们发现 the following paragraph:
If the call would <...> find a better match had the lookup within the associated namespaces considered all the function declarations with external linkage introduced in those namespaces in all translation units, not just considering those declarations found in the template definition and template instantiation contexts, then the program has undefined 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)
会更好,但在模板实例化上下文中找不到)