title: When is there an UB because the best overload match was not found by ADL at the point of instantiation?
body: <p>When a function body is instantiated, dependent function call overload resolution should find the best match in associated namespace through ADL, otherwise the behavior is undefined, <a href="https://timsong-cpp.github.io/cppwp/n4861/temp.dep.candidate#1.sentence-3" rel="nofollow noreferrer">[temp.dep.candidate]§1</a></p>
  <blockquote>
  <p>If the call would be ill-formed or would find a better match had the lookup within the associated namespaces considered <strong>all the function declarations with external linkage introduced in those namespaces</strong> in all translation units, not just considering those declarations found in the template definition and template instantiation contexts, then the program has undefined behavior.</p>
  </blockquote>
  <p>For exemple:</p>
  <pre><code>struct A {
      friend void f(A a){};
  };
  
  struct B : A {};
  
  template &lt;class T&gt; 
  void g (T a) {
      f(a);
  }
  
  void h(B b){
      g(b);//undefined behavior (UB)
  }
   //TU2.cpp
   //...
  void f(B){}
  </code></pre>
  <p>I have 2 doubts about which functions could be one of <em>all the function declarations with external linkage introduced in those namespaces</em></p>
  <p><strong>Question 1</strong></p>
  <p>Does inline friend function considered to be introduced in the enclosing namespace?</p>
  <p>Exemple:</p>
  <pre><code>struct A {
      friend void f(A a){};
  };
  
  struct B : A {};
  
  template &lt;class T&gt; 
  void g (T a) {
      f(a);
  }
  
  void h(B b){
      g(b);//undefined behavior ??
  }
  //TU2.cpp
  //....
  struct C {
     operator B();
     friend void f(B){}
     };
  </code></pre>
  <p><strong>Question 2</strong></p>
  <p>Could function template specialization also trigger this undefined behavior? (Potentialy instantiated or instantiated function template specialization)</p>
  <p>Exemple:</p>
  <pre><code>struct A {
      friend void f(A a){};
  };
  
  struct B : A {};
  
  template &lt;class T&gt; 
  void g (T a) {
      f(a);
  }
  
  void h(B b){
      g(b);//undefined behavior ??
  }
  //TU2.cpp
  //...
  template &lt;class T&gt; void f(T){}
  
  // optionaly also with:
  // template void f(B);
  </code></pre>