title: How to properly define friend free functions
body: <h2>Background</h2>
  
  <p>I'm trying to write a container which will contain iterators. E.g. it creates indirection range. I want the iterator of the range to dereference underlying iterator. I did that, but then I need relational operators. Here is the reduced version that reproduces the problem:</p>
  
  <h2>mcve</h2>
  
  <pre><code>template &lt;typename T&gt;
  struct foo
  {
      class bar
      {
          friend bool do_something(bar);
      };
  
      bar get_bar()
      {
          return bar{};
      }
  };
  
  template &lt;typename T&gt;
  bool do_something(typename foo&lt;T&gt;::bar)
  {
      return true;
  }
  
  #include &lt;iostream&gt;
  
  int main()
  {
      foo&lt;int&gt; f;
      auto b = f.get_bar();
      std::cout &lt;&lt; do_something(b);
  }
  </code></pre>
  
  <blockquote>
    <p>/tmp/prog-c08a3a.o: In function <code>main':
    prog.cc:(.text+0x12): undefined reference to</code>do_something(foo::bar)'</p>
  </blockquote>
  
  <p><a href="https://wandbox.org/permlink/WorRnCrAzosI93vk" rel="nofollow noreferrer">demo on wandbox</a>.</p>
  
  <p>This is the only error I get.</p>
  
  <h2>Question</h2>
  
  <p>What is declared inside of the class and what is defined as free function outside of the class?</p>
  
  <h2>What I tried</h2>
  
  <p>Compiler asks me to put a template, but that is clearly not what I want. I don't want my iterator related to one <code>ForwardIterator</code> type to be comparable to iterator of another <code>ForwardIterator</code> type. But I thought ok, lets try that, but it gave me shadowing error, which strengthened my suspicions.</p>
  
  <h2>Reasons why I want to define inside</h2>
  
  <p>From what I've heard, defining it inside will make it only ADL callable. Some IDEs might not figure that out, so I thought that declaring them outside is better.</p>