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

标签 c++ templates language-lawyer

考虑以下片段:

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 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) 会更好,但在模板实例化上下文中找不到)

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

相关文章:

c++ - 在派生类中创建具有相同名称但不同返回类型的新函数

c++ - 哔哔声不起作用(linux)

c++ - 为什么 C++ Map 的 [] 运算符调用映射值的默认构造函数?

c++ - Union 在 OOP 中的使用

c++ - 使用空指针参数和不可能的后置条件构造标准异常

c++ - 在阅读 callgrind 输出时需要帮助

c++ - 使用静态二维数组定义类的便捷方法是什么(二维数组的大小仅在编译时已知)?

c++ - 具有未使用模板参数的函数模板

css - django 模板中的行

c++ - 移动 std::bitset<N> 是否超过 N 个位置未定义行为?