<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C# for C++ Devs: Generics vs Templates for Primitive Types</title><link>http://blogs.msdn.com/b/colinth/archive/2007/07/02/c-for-c-users-generics-vs-templates-for-primitive-types.aspx</link><description>I was trying to write some type-generic (almost) code in C# using a pattern that I commonly use in C++. A very simple version of what I was trying to do looks something like: 
 
 class B {}; 
 template &amp;lt; typename T&amp;gt; 
 int convert(T value) </description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: C# for C++ Devs: Generics vs Templates for Primitive Types</title><link>http://blogs.msdn.com/b/colinth/archive/2007/07/02/c-for-c-users-generics-vs-templates-for-primitive-types.aspx#7440931</link><pubDate>Mon, 04 Feb 2008 19:37:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7440931</guid><dc:creator>luksan</dc:creator><description>&lt;p&gt;This error is due to the compile-time check of convertibility. However, this check can be circumvented so that only run-time checking takes place by first converting &amp;nbsp;your type to &amp;quot;object&amp;quot;. So, return (int)(object)value; would have worked just as well.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7440931" width="1" height="1"&gt;</description></item><item><title>re: C# for C++ Users: Generics vs Templates for Primitive Types</title><link>http://blogs.msdn.com/b/colinth/archive/2007/07/02/c-for-c-users-generics-vs-templates-for-primitive-types.aspx#3672929</link><pubDate>Tue, 03 Jul 2007 19:58:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3672929</guid><dc:creator>Colin Thomsen - MSFT</dc:creator><description>&lt;p&gt;You're right and it does solve my problem. I was still thinking like a C++ dev where primitive types are not classes themselves.&lt;/p&gt;
&lt;p&gt;I did a little extra reading (&lt;a rel="nofollow" target="_new" href="http://msdn2.microsoft.com/en-us/library/s1ax56ch"&gt;http://msdn2.microsoft.com/en-us/library/s1ax56ch&lt;/a&gt;(VS.80).aspx) and all the primitive (or simple) types are merely aliases for classes in the System namespace. For example, int is an alias for System.Int32. That class implements the IConvertible interface so I can use the constraint-based convert you list above.&lt;/p&gt;
&lt;p&gt;Thanks for the suggestion.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3672929" width="1" height="1"&gt;</description></item><item><title>re: C# for C++ Users: Generics vs Templates for Primitive Types</title><link>http://blogs.msdn.com/b/colinth/archive/2007/07/02/c-for-c-users-generics-vs-templates-for-primitive-types.aspx#3670769</link><pubDate>Tue, 03 Jul 2007 16:23:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3670769</guid><dc:creator>Alan J. McFarlane</dc:creator><description>&lt;p&gt;I'm not a C++ user so I may be missing something, but your simple example can be done as follows:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; static int convert(IConvertible value)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;return value.ToInt32(null);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;IConvertible is implemented by all the primitive types.&lt;/p&gt;
&lt;p&gt;There's a boxing operation when calling that method (except when it's inlined by the JIT?), the following removes that:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; static int convert2&amp;lt;T&amp;gt;(T value)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; where T: IConvertible&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;return value.ToInt32(null);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;Alan&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3670769" width="1" height="1"&gt;</description></item></channel></rss>