GCC unexpected behaviour when selecting function overload in C++
<p>Consider the following example:</p>

<pre><code>#include &lt;string&gt;
#include &lt;iostream&gt;

namespace nr {

  template &lt;typename T&gt;
  static inline std::string tostr(T const&amp; v)
  {
    return std::to_string(v);
  }

}

namespace nr {
namespace logging {

  enum Level {
    kDebug,
    kInfo,
    kWarning,
    kError
  };


  template &lt;typename T&gt;
  static inline void log(std::string const&amp; message, T const&amp; v)
  {
    std::cout &lt;&lt; message &lt;&lt; nr::tostr(v) &lt;&lt; "\n";
  }

}
}

namespace nr {

  static inline std::string tostr(nr::logging::Level level)
  {
    switch (level) {
      case nr::logging::kDebug:
        return "DEBUG";
      case nr::logging::kInfo:
        return "INFO";
      case nr::logging::kWarning:
        return "WARNING";
      case nr::logging::kError:
        return "ERROR";
      default:
        return "UNKNOWN_LEVEL";
    }
  }

}

int main()
{
  nr::logging::log("Hello", nr::logging::kInfo);
  nr::logging::log("Hello", nr::logging::kWarning);
}
</code></pre>

<p>This is the output compiled with GCC 5.2.1 on Ubuntu 15.10</p>

<pre><code>Hello1
Hello2
</code></pre>

<p>What I would expect it to output, and it does so on Windows with MSVC 2013:</p>

<pre><code>HelloINFO
HelloWARNING
</code></pre>

<hr>

<p>I have found ways to fix it, but I do not understand why there are different behaviours! As far as I understand it, the <code>nr::logging::log()</code> template is instantiated at the point the function is called for the first time.  At that very point, <code>nr::tostr(nr::logging::Level)</code> is already defined, thus it should select this function in <code>nr::logging::log()</code>.</p>

<p>Why is there a different behaviour depending on the order that the functions are declared in or whether they are in the global namespace or not?</p>

<p>Things to consider that puzzle me:</p>

<ul>
<li>Why does snippet #3 work as I expect it while snippet #2 does not? The only difference is that the functions are all in the same but their own namespace instead of the global namespace.</li>
<li>In reply to @juanchopanza 's comment, if the function overload <code>nr::tostr(nr::logging::Level)</code> does not exist at the time <code>nr::log()</code> is called, why does snippet #3 do what I expect it to? The order is still the same (the function overload being declared after <code>nr::log()</code>)</li>
</ul>

<hr>

<ul>
<li><a href="http://ideone.com/570ZwR" rel="nofollow">#1 Original Code *</a></li>
<li><a href="http://ideone.com/Zuzld0" rel="nofollow">#2 Put everything in <code>nr</code> namespace *</a></li>
<li><a href="http://ideone.com/Yhlafs" rel="nofollow">#3 Put everything in global namespace</a></li>
<li><a href="http://ideone.com/0eGe5R" rel="nofollow">#4 Declare <code>nr::tostr(nr::logging::Level)</code> before <code>nr::logging::log()</code></a></li>
</ul>

<p><em>Snippets marked with * do NOT work as I expected it</em></p>