title: C++ Template Method Type Constrained
body: <p>I have to create a template function, which its T value is child of a base class (Actor). In C# it is done like this:</p>
  
  <pre><code>//C#
  public T Create&lt;T&gt;() where T : Actor
  {
  }
  </code></pre>
  
  <p>If you take a look at the C# MSD (<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint" rel="nofollow noreferrer">here</a>):</p>
  
  <blockquote>
    <p>The <strong>where</strong> clause in a generic definition specifies constraints on
    the types that are used as arguments for type parameters in a generic
    type, method, delegate, or local function. [<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint" rel="nofollow noreferrer">...</a>]</p>
  </blockquote>
  
  <p>So, I tried this into my C++ code but I was wondering to find other ways to make it. Something more noticeable that doesn't allow you to give this function an unsupported value and/or, in case you do it, you detect the error easily. This way isn't working fine for me.</p>
  
  <pre><code>//C++
  template&lt;typename T&gt;
  T* Create()
  {
      static_assert(std::is_base_of&lt;Actor, T&gt;::value, L"T must inherit from Actor");
  
      //Some code
      return actor;
  }
  </code></pre>
  
  <p>Thanks in advance and a greeting!</p>