title: function specialization based on if template parameter is shared_ptr
body: <p>I'm trying to detect if a type is a <code>shared_ptr&lt;T&gt;</code> and if it is, dispatch to a specific function template or override.</p>
  
  <p>Here's a simplified version of what I'm actually attempting:
  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;type_traits&gt;
  #include &lt;memory&gt;
  #include &lt;cstdio&gt;
  
  template &lt;class T&gt; struct is_shared_ptr : std::false_type {};
  template &lt;class T&gt; struct is_shared_ptr&lt;std::shared_ptr&lt;T&gt; &gt; : std::true_type {};
  
  class Foo { };
  typedef std::shared_ptr&lt;Foo&gt; SharedFoo;
  
  template&lt;class T&gt; void getValue();
  
  template&lt;class T, typename std::enable_if&lt;is_shared_ptr&lt;T&gt;::value&gt;::type = 0&gt;
  void getValue()
  {
      printf("shared!\n");
  }
  
  template&lt;class T, typename std::enable_if&lt;!is_shared_ptr&lt;T&gt;::value&gt;::type = 0&gt;
  void getValue()
  {
      printf("not shared!\n");
  }
  
  int main(int, char **)
  {
      getValue&lt;SharedFoo&gt;();
      getValue&lt;Foo&gt;();
      return 0;
  }
  </code></pre>
  
  <p>It compiles just fine, but it seems the actual functions were never actually generated because the code doesn't link with the following errors:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>/tmp/ccjAKSBE.o: In function `main':
  shared_test.cpp:(.text+0x10): undefined reference to `void getValue&lt;std::shared_ptr&lt;Foo&gt;&gt;()'
  shared_test.cpp:(.text+0x15): undefined reference to `void getValue&lt;Foo&gt;()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I would think that those would be covered by the two function templates. But they aren't. </p>
  
  <p>Given that, it seems I am seriously misunderstanding something.</p>
  
  <p>So maybe it would help if I explain what I'm /trying/ to do rather than what I'm actually doing.</p>
  
  <p>I have some "magic" code using a bunch of new (to me) C++11 features to bind C++ code to lua (can be seen here: <a href="https://github.com/Tomasu/LuaGlue" rel="nofollow noreferrer">https://github.com/Tomasu/LuaGlue</a>). someone has recently asked for support for binding to classes wrapped in <code>shared_ptr</code>'s. which is not something that works at the moment, because it binds at compile time using templates and tuple unwrapping to generate code to call functions on either the C++ or lua side. In the "magic" unwrapping code, I have a bunch of overridden and "specialized" functions that handle various variable types. Some for basic types, one for static objects, and another for pointer to objects. A <code>shared_ptr</code> can't be handled in the same way as either a static or pointer object, so I need to add some extra handling just for them.</p>
  
  <p>For example:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template&lt;typename T&gt;
  T getValue(LuaGlue &amp;, lua_State *, unsigned int);
  
  template&lt;&gt;
  int getValue&lt;int&gt;(LuaGlue &amp;, lua_State *state, unsigned int idx)
  {
      return luaL_checkint(state, idx);
  }
  
  template&lt;class T&gt;
  T getValue(LuaGlue &amp;g, lua_State *state, unsigned int idx)
  {
      return getValue_&lt;T&gt;(g, state, idx, std::is_pointer&lt;T&gt;());
  }
  </code></pre>
  
  <p>That's the actual code (notice the hairy template/override via function argument :-x).</p>
  
  <p>I had thought it'd be as simple as adding another <code>addValue</code> function, along the lines of the code in my earlier example, via <code>enable_if</code>.</p>