title: Test to detect if a template type in C++/CLI is managed or unmanaged
body: <p>I have some template code compiled with /clr, something like the following</p>
  
  <pre><code>template &lt;typename T&gt; void foo( )
  {
  }
  </code></pre>
  
  <p>and am wondering if it's possible to detect if the T is a managed type or an unmanaged one. So something like</p>
  
  <pre><code>template &lt;typename T&gt; void foo( )
  {
       constexpr bool b = is_managed_type&lt;T&gt;;
  }
  </code></pre>
  
  <p>If have tried all the std library type traits and those mentioned here <a href="https://docs.microsoft.com/en-us/cpp/extensions/compiler-support-for-type-traits-cpp-component-extensions?view=vs-2019" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/cpp/extensions/compiler-support-for-type-traits-cpp-component-extensions?view=vs-2019</a> but have not found anything that works in all cases. A particular instance of this problem is to tell if an enum is a managed enum or an unmanaged one. So is it possible to implement a type trait like this?</p>
  
  <p>Added 9/23/19
  I did make some progress on a is_managed_type trait. I have the following</p>
  
  <pre><code>int check( System::Object^ );
  
  template &lt;typename T, typename U = void&gt; using converts_to_system_object_t = decltype(check(std::declval&lt;T&gt;()));
  
  
  template&lt;typename T, typename = void&gt; struct is_managed_type: std::false_type {};
  
  template&lt;typename T&gt; struct is_managed_type&lt;T, converts_to_system_object_t&lt;T&gt;&gt; : std::true_type { };
  </code></pre>
  
  <p>The basic idea is to test if for given type can you call a function that takes os System::Object^. Note I did try things like std::is_base_of and std::is_convertible but they all had problems.
  However this seem to return false for managed enums which is a bit bizzare to me</p>