title: Unified function calls for instances and primitive types using template specialization and interfaces
body: <p>I'd like to call a function <code>AsJson</code> in a unified way irrespective of whether I'm dealing with an instance or a primitive type, or anything else.</p>
  
  <p>I thought I could define an abstract base class which classes could inherit, and then define a template function that calls the <code>AsJson</code> member function when the template is specialized to appropriate classes. For other types, they could just specialize the template function.</p>
  
  <p>Something like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class IInstrumented {
   public:
    virtual void AsJson() const = 0;
  };
  
  template &lt;typename T&gt;
  void AsJson(const T&amp;);
  
  template &lt;typename T&gt;
  void AsJson(const IInstrumented&amp; instrumented) {
    instrumented.AsJson();
  }
  
  class Foo : public IInstrumented {
   public:
    void AsJson() const override { std::cout &lt;&lt; "A Foo!" &lt;&lt; std::endl; }
  };
  
  template &lt;&gt;
  void AsJson(const int&amp; x) {
    std::cout &lt;&lt; "An integer!" &lt;&lt; std::endl;
  }
  
  int main() {
    Foo foo;
    AsJson(foo);
  
    int x = 3;
    AsJson(x);
  }
  </code></pre>
  
  <p>Unfortunately, this results in the following linker error:</p>
  
  <pre><code>special.cpp:(.text+0x56): undefined reference to `void AsJson&lt;Foo&gt;(Foo const&amp;)'
  </code></pre>
  
  <p>Is this approach workable as is? Is the fix something relatively minor, or is an entirely different approach warranted?</p>