title: Is it possible to write a C++ template to check for a constructor's existence?
body: <p>This question is in spirit a follow-on from this question from another user, which has some excellent answers:
  <a href="https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence">Is it possible to write a template to check for a function&#39;s existence?</a></p>
  
  <p>I want to do exactly what is described in this question, except I want to be able to do it for a constructor. Eg, given these two types:</p>
  
  <pre><code>class NormalType
  {
  public:
      NormalType()
      {
          std::cout &lt;&lt; "NormalType::NormalType()" &lt;&lt; std::endl;
      }
  };
  
  class SpecialType
  {
  public:
      SpecialType()
      {
          std::cout &lt;&lt; "SpecialType::SpecialType()" &lt;&lt; std::endl;
      }
      SpecialType(int someArg)
      {
          std::cout &lt;&lt; "SpecialType::SpecialType(int someArg)" &lt;&lt; std::endl;
      }
  };
  </code></pre>
  
  <p>And this helper function for constructing an object:</p>
  
  <pre><code>template&lt;class T&gt;
  class ConstructHelper
  {
  public:
      template&lt;bool HasSpecialConstructor&gt;
      static T Construct()
      {
          return T();
      }
      template&lt;&gt;
      static T Construct&lt;true&gt;()
      {
          return T(int(42));
      }
  };
  </code></pre>
  
  <p>I want to be able to write code like this:</p>
  
  <pre><code>NormalType normalType = ConstructHelper&lt;NormalType&gt;::Construct&lt;has_special_constructor&lt;NormalType&gt;::value&gt;();
  SpecialType specialType = ConstructHelper&lt;SpecialType&gt;::Construct&lt;has_special_constructor&lt;SpecialType&gt;::value&gt;();
  </code></pre>
  
  <p>Where the desired results are that <code>NormalType::NormalType()</code> is called, and <code>SpecialType::SpecialType(int someArg)</code> is called. The missing ingredient here is that critical <code>has_special_constructor</code> helper, which can determine if our special constructor exists for a given type.</p>
  
  <p>The previous question I referenced deals with checking if a given function exists on a type, and there were a variety of working solutions presented. Unfortunately, most of them rely on being able to take the address of the target method, and as per the C++ spec, you can't take the address of a constructor (12.1.10). Of the remaining working solutions, all of them seem to rely on SFINAE with decltype on an arbitrary expression in a template specialization. That is a simple way to solve this problem, but unfortunately I'm working on Visual Studio 2013, which doesn't properly support the C++11 SFINAE rules, and still won't with the release of "Visual Studio 14" either. With proper SFINAE support, I should be able to do this for example:</p>
  
  <pre><code>template&lt;class T&gt;
  struct has_special_constructor
  {
      template&lt;class S&gt;
      struct calculate_value: std::false_type {};
      template&lt;&gt;
      struct calculate_value&lt;decltype(T(int(42)))&gt;: std::true_type {};
  
      static const bool value = calculate_value&lt;T&gt;::value;
  };
  </code></pre>
  
  <p>But that won't compile under VS2013 if I try and test a type that doesn't define my target constructor, due to the lack of SFINAE support. That said, I'm not yet convinced this is impossible, I think there might be a way to make it work, but I haven't been able to find a solution so far. Anyone out there see a way to do this that I've overlooked?</p>
  
  <p>Here's some more info about what I'd need in order to accept an answer as a solution to this problem:</p>
  
  <ul>
  <li><p>It must be possible to resolve <code>has_special_constructor&lt;T&gt;::value</code> for any given type without additional code being written for that specific type.</p></li>
  <li><p>I'm primarily targeting Visual Studio 2013, so any solution must work in that environment. I know a fully conforming C++11 compiler could manage this problem more easily, but I'm looking for something that can function now on my current target compiler.</p></li>
  <li><p>If anyone can supply a solution that works in a C++03 compiler (IE, without any C++11 features) I'll accept that over one that uses C++11 features.</p></li>
  <li><p>I'd settle for a MSVC extension-based workaround at this point, since I can use the preprocessor to fall back to this method until full C++11 support comes along.</p></li>
  </ul>