<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marius Bancila's Blog &#187; CTP</title>
	<atom:link href="http://mariusbancila.ro/blog/tag/ctp/feed/" rel="self" type="application/rss+xml" />
	<link>http://mariusbancila.ro/blog</link>
	<description>Sharing my opinions and ideas!</description>
	<lastBuildDate>Fri, 06 Apr 2012 13:45:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Hacking WCF Extensions for Visual Studio 2005</title>
		<link>http://mariusbancila.ro/blog/2009/10/28/hacking-wcf-extensions-for-visual-studio-2005/</link>
		<comments>http://mariusbancila.ro/blog/2009/10/28/hacking-wcf-extensions-for-visual-studio-2005/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 22:43:27 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CTPs & Betas]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[MSI]]></category>
		<category><![CDATA[Orca]]></category>
		<category><![CDATA[VS2005]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=373</guid>
		<description><![CDATA[If you are still using Visual Studio 2005 and need to develop WCF services you need the following: Microsoft .NET Framework 3.0 Redistributable Package (SP2) The Visual Studio 2005 extensions for.NET Framework 3.0 (WCF &#038; WPF), November 2006 CTP 3 The problem with the later is that Microsoft no longer supports it. Visual Studio 2008 [...]]]></description>
			<content:encoded><![CDATA[<p>If you are still using Visual Studio 2005 and need to develop WCF services you need the following:</p>
<ul>
<li><a href="http://www.microsoft.com/downloads/details.aspx?familyid=10cc340b-f857-4a14-83f5-25634c3bf043&#038;displaylang=en" target="_blank">Microsoft .NET Framework 3.0 Redistributable Package (SP2)</a></li>
<li>The Visual Studio 2005 extensions for.NET Framework 3.0 (WCF &#038; WPF), November 2006 CTP 3</li>
</ul>
<p>The problem with the later is that Microsoft no longer supports it. Visual Studio 2008 is supposed to be used for developing such projects. The license for the CTP has expired on June 30th 2008. You can read more about that <a href="http://blogs.msdn.com/acangialosi/archive/2008/06/27/vs-2005-extensions-for-net-framework-3-0-wpf-wcf-ctp-is-coming-off-the-ms-download-center.aspx" target="_blank">here</a>. <b>What that means is that you can no longer develop WCF applications in Visual Studio 2005 and should upgrade to Visual Studio 2008.</b> However, as an exercise, I wanted to see if I could still install the WCF extensions in Visual Studio 2005 and have it work side by side with Visual Studio 2008.</p>
<p>First, I had to find the old CTP with the Visual Studio 2005 extensions. It can be downloaded from <a href="http://download.cnet.com/The-Visual-Studio-2005-extensions-for-NET-Framework-3-0-WCF-WPF-November-2006-CTP/3000-10250_4-10727672.html" target="_blank">here</a>. However, when I run it, I got the following error:</p>
<div class="wp-caption alignnone" style="width: 282px"><img alt="WCF Extensions for Visual Studio 2005 Setup Error" src="/blog/wp-content/uploads/2009/10/wcfext_setuperror.png" title="Setup Error" width="272" height="232" /><p class="wp-caption-text">WCF Extensions for Visual Studio 2005 Setup Error</p></div>
<p>The curious thing was that .NET 3.0 was already installed on my machine. I soon realized I was having .NET 3.0 SP2, and the installer was looking for .NET 3.0. Of course, you cannot install 3.0 when a newer version (such as 3.0 SP2) is already installed. So the only option was to change the installer.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/aa370557%28VS.85%29.aspx" target="_blank">Orca</a> is a database editor, allowing you to create and edit MSI files and merge modules. It is provided as a part of <a href="http://www.microsoft.com/downloadS/details.aspx?FamilyID=e96f8abc-62c3-4cc3-93ad-bfc98e3ae4a3&#038;displaylang=en" target="_blank">Windows Installer SDK</a>. But you can also find stand alone downloads, such as <a href="http://www.softpedia.com/get/Authoring-tools/Setup-creators/Orca.shtml" target="_blank">this</a>.</p>
<p>After installing Orca, you can open the MSI file. From Tools > Validate&#8230; one can run a validation on the installer. It shown the following errors:</p>
<blockquote><p>ICE08	ERROR	Component: WCFSvcConfigEditor_dll has a duplicate GUID: {714D044E-3136-457E-ADD7-AE3D0FEF021A}<br />
ICE16	ERROR	ProductName: &#8216;Visual Studio 2005 extensions for .NET Framework 3.0 (WCF &#038; WPF), November 2006 CTP&#8217; is greater than 63 characters in length. Current length: 83<br />
ICE77	ERROR	CA_CommitHelpTransactionNoRB.3643236F_FC70_11D3_A536_0090278A1BB8 is a in-script custom action.  It must be sequenced in between the InstallInitialize action and the InstallFinalize action in the InstallExecuteSequence table
</p></blockquote>
<p>I had to do the following fixes:</p>
<ul>
<li>replace the first occurrence of this GUID {714D044E-3136-457E-ADD7-AE3D0FEF021A} with another one</li>
<li>trim the name &#8220;Visual Studio 2005 extensions for .NET Framework 3.0 (WCF &#038; WPF), November 2006 CTP&#8221; to &#8220;Visual Studio 2005 extensions for .NET Framework 3.0 SP2&#8243;</li>
<li>change the Sequence number of the CA_CommitHelpTransactionNoRB.3643236F_FC70_11D3_A536_0090278A1BB8 action in he InstallExecuteSequence table from 6601 to 6599 so that it is in between InstallInitialize (1500) and InstallFinalize (6600)</li>
</ul>
<p>But then, came the biggest problem: making the installer work with .NET 3.5 SP2. It was looking in registry for the following key: SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{15095BF3-A3D7-4DDF-B193-3A496881E003}. The GUID {15095BF3-A3D7-4DDF-B193-3A496881E003} corresponds to .NET 3.0. So in order to make it work with .NET 3.0 SP2 one needs to replace the GUID with {A3051CD0-2F64-3813-A88D-B8DCCDE8F8C7} for the SearchForWinFXruntimeX86Install signature in the RegLocator table.</p>
<p>Once those changes are made all you have to do is save. However, the &#8220;Copy embedded steams during &#8216;Save As&#8217;&#8221; option should be checked from Tools > Options > Database, before doing the saving. Otherwise the embedded CAB is not copied and the installation won&#8217;t work.</p>
<p>Running the modified installer works successfully.<br />
<div class="wp-caption alignnone" style="width: 513px"><img alt="Setup start page" src="/blog/wp-content/uploads/2009/10/wcfext_setupok.png" title="Setup start" width="503" height="414" /><p class="wp-caption-text">Setup start page</p></div></p>
<div class="wp-caption alignnone" style="width: 513px"><img alt="Setup finished" src="/blog/wp-content/uploads/2009/10/wcfext_setupfinished.png" title="Setup finished" width="503" height="414" /><p class="wp-caption-text">Setup finished</p></div>
<p>If you start Visual Studio 2005 you can create a new project from one of the WCF project templates.<br />
<div class="wp-caption alignnone" style="width: 691px"><img alt="WCF Project Templates" src="/blog/wp-content/uploads/2009/10/wcfext_vs2005.png" title="WCF Project Templates" width="681" height="469" /><p class="wp-caption-text">WCF Project Templates</p></div></p>
<p>You can download the altered MSI file from here: <a class="downloadlink" href="http://mariusbancila.ro/blog/wp-content/plugins/download-monitor/download.php?id=5" title="Version1.0 downloaded 1688 times" >The Visual Studio 2005 extensions for.NET Framework 3.0 SP2 (WCF &amp; WPF) (1688)</a>.</p>
<p><b>Note: I must say this again: this scenario is no longer supported. The license for this CTP for WCF extensions for Visual Studio 2005 has expired in 2008. You should upgrade to Visual Studio 2008 to develop WCF applications.</b></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/10/28/hacking-wcf-extensions-for-visual-studio-2005/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ static_assert, a niche feature</title>
		<link>http://mariusbancila.ro/blog/2009/02/25/c-static_assert-a-niche-feature/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/25/c-static_assert-a-niche-feature/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 06:00:14 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[CTPs & Betas]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[assert]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[static_assert]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=184</guid>
		<description><![CDATA[The new C++ standard defines a new keyword, static_assert, that is already available in Visual Studio 2010 CTP. This new feature allows introducing compile time asserts. It takes an expression that can evaluate to bool and a string. If the expression evaluates to false, the compiler issues an error with the given string literal. If [...]]]></description>
			<content:encoded><![CDATA[<p>The new C++ standard defines a new keyword, <b>static_assert</b>, that is already available in Visual Studio 2010 CTP. This new feature allows introducing compile time asserts. It takes an expression that can evaluate to bool and a string. If the expression evaluates to false, the compiler issues an error with the given string literal. If the expression evaluates to true, static_assert has no effect.</p>
<p>Here is an example for using static_assert. Suppose you want to create a template vector class, but you don&#8217;t want to allow vectors with a size smaller than 4. Then you can use a static assertion to enforce that.</p>
<pre class="prettyprint">
template < class T, int Size >
class Vector
{
   static_assert(Size > 3, "Vector size is too small!");

   T m_values[Size];
};

int _tmain(int argc, _TCHAR* argv[])
{
   Vector< int, 4 > four;
   Vector< short, 2 > two;

   return 0;
}
</pre>
<p>Compiling this program triggers an error at the second declaration.</p>
<pre class="prettyprint">
c:\projects\cpp_demo\cpp_demo.cpp(17) : error C2338: Vector size is too small!
  c:\projects\cpp_demo\cpp_demo.cpp(33) : see reference to class template instantiation 'Vector< T,Size >'
  being compiled
  with
  [
     T=short,
     Size=2
  ]
</pre>
<p>Most of the use cases for this feature, in my opinion, would be to test on the size of different types. For instance this sample is taken from the C++0x draft.</p>
<pre class="prettyprint">
static_assert(sizeof(long) >= 8, "64-bit code generation required for this library.");
</pre>
<p>To me, static_assert looks like a niche feature. It would have been great if this could be used together with some other features to enfore compile time constraints on types. For instance, I want to restrict the possible types for a template class or function to only those that are derived from IListener (a fictive class).</p>
<pre class="prettyprint">
template < class T >
class foo
{
   static_assert(convertible_from< IListener >(decltype(T)), "Type is not a correct type!");
};
</pre>
<p>Perhaps a future standard version will offer support for such things.</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/25/c-static_assert-a-niche-feature/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lambdas in C++</title>
		<link>http://mariusbancila.ro/blog/2009/02/24/lambdas-in-c/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/24/lambdas-in-c/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 07:00:26 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[CTPs & Betas]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[aggregate]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[function object]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=179</guid>
		<description><![CDATA[The new C++0x standard adds lambda expressions to the language. Visual Studio 2010 CTP is already supporting this new feature that brings functional techniques to C++ too. What is a lambda expression? It&#8217;s basically a function. In F# it&#8217;s an anonymous function, in C# it&#8217;s an anonymous delegate; in C++ it&#8217;s in fact an anonymous [...]]]></description>
			<content:encoded><![CDATA[<p>The new C++0x standard adds lambda expressions to the language. Visual Studio 2010 CTP is already supporting this new feature that brings functional techniques to C++ too.</p>
<p>What is a lambda expression? It&#8217;s basically a function. In F# it&#8217;s an anonymous function, in C# it&#8217;s an anonymous delegate; in C++ it&#8217;s in fact an anonymous function object. Whenever you create a new lambda function, the compiler creates a function object for you.</p>
<pre class="prettyprint">
int main()
{
   auto l_pow2 = [](int n) {return n*n;};

   std::cout << "5 pow 2 = " << l_pow2(5) << std::endl;
}
</pre>
<p>That is equivalent to:</p>
<pre class="prettyprint">
struct LambdaFunctor
{
   int operator()(int n) const
   {
      return n * n;
   }
};

int main()
{
   LambdaFunctor l_pow2;
   std::cout << "5 pow 2 = " << l_pow2(5) << std::endl;
}
</pre>
<p>Of course, the lambda functor can be more complicated, when the lambda function captures state from the local scope. However, that is beyond the scope of my post. I recommend that you read more about lambdas in C++ on the <a href="http://blogs.msdn.com/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0x-features-in-vc10-part-1.aspx" target="_blank">VC++ blog</a>.</p>
<p>Question is, what are these lambdas good for? Well, they mostly come in handly with algorithms that take predicates (function objects) as arguments. I'll try to give you some examples in this post.</p>
<p>Let's first consider a filter function, that takes a sequence (vector of T) and a predicate that indicates what values should be filtered, and returns a new sequence. That would look like this:</p>
<pre class="prettyprint">
template < class T >
std::vector< T > Filter(const std::vector< T >&amp; sequence,
                        std::tr1::function< bool (T) > predicate)
{
   std::vector< T > result;

   for(auto it = sequence.begin(); it != sequence.end(); ++it)
      if(predicate(*it))
         result.push_back(*it);

   return result;
}
</pre>
<p>We can use this filter function to extract the odds numbers from a sequence (vector).</p>
<pre class="prettyprint">
#include < iostream >
#include < vector >
#include < algorithm >
#include < functional >

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   std::vector< int > nums;
   for(int i = 0; i < 10; ++i)
      nums.push_back(i);

   // get the odds numbers
   std::vector< int > odds = Filter< int >(nums, [](int i) {return (i % 2) == 1;});

   // print the new sequence
   for_each(odds.begin(), odds.end(), [](int n){std::cout << n << std::endl;});

   return 0;
}
</pre>
<pre class="prettyprint">
1
3
5
7
9
</pre>
<p>You could see in the above example that a second lambda function is used for printing the numbers to the console.</p>
<p>Since the Filter function is a template function it could be used with other types too. In the next example we'll see how to filter words that have at least 4 letters from a sequence of words.</p>
<pre class="prettyprint">
#include < iostream >
#include < vector >
#include < algorithm >
#include < functional >
#include < string >

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   std::vector< string > snums;
   snums.push_back("one");
   snums.push_back("two");
   snums.push_back("three");
   snums.push_back("four");
   snums.push_back("five");

   // filter the words, notice the new lambda
   std::vector< string > bigwords = Filter< string >(snums, [](string w) {return w.length() > 3;});

   // print the selected words
   for_each(bigwords.begin(), bigwords.end(), [](string s){std::cout << s << std::endl;});

   return 0;
}
</pre>
<pre class="prettyprint">
three
four
five
</pre>
<p>Let's consider a second example, a Find (template) function, that takes a sequence and a predicate (that checks a condition for an element), and returns the first element in the sequence for which the predicate returned true.</p>
<pre class="prettyprint">
template < class T >
T Find(const std::vector< T >&amp; sequence,
       std::tr1::function< bool (T) > predicate)
{
   for(auto it = sequence.begin(); it != sequence.end(); ++it)
      if(predicate(*it))
         return *it;

   throw std::runtime_error("Item not found");
}
</pre>
<p>We'll use this function to find the first element in a sequence that is greater than a given value.</p>
<pre class="prettyprint">
int _tmain(int argc, _TCHAR* argv[])
{
   std::vector< int > nums;
   nums.push_back(1);
   nums.push_back(3);
   nums.push_back(5);
   nums.push_back(7);
   nums.push_back(9);

   int min;
   cout << "find first after: ";
   cin >> min;

   try
   {
      int val = Find< int >(odds, [min](int i){return i > min;});
      cout << val << endl;
   }
   catch(std::runtime_error&amp; ex)
   {
      cout << ex.what() << endl;
   }

   return 0;
}
</pre>
<p>If you input 4 for instance, it will return 5. If you input 10, an exception will be thrown. You can see that this time the lambda function is <b>[min](int i){return i &gt; min;}</b>. This means that it captures by value the min variable from the local scope, so that it can compare each element with that given value.</p>
<p>The last example I'm going to show is an accumulator function (also known as aggregate or fold). This function takes a sequence of elements, a seed (or initial value) and a function that specifies how to aggregate the elements, and returns the aggregate.</p>
<pre class="prettyprint">
template < class TSource, class TAccumulate >
TAccumulate Aggregate(const std::vector< TSource >&amp; sequence,
                      TAccumulate seed,
                      std::tr1::function< TAccumulate (TSource, TAccumulate) > func)
{
   TAccumulate acc = seed;
   for(auto it = sequence.begin(); it != sequence.end(); ++it)
      acc = func(acc, *it);

   return acc;
}
</pre>
<p>First, we can use it to compute the sum of all elements in a sequence.</p>
<pre class="prettyprint">
int _tmain(int argc, _TCHAR* argv[])
{
   std::vector< int > nums;
   for(int i = 1; i <= 10; ++i)
      nums.push_back(i);

   int sum = Aggregate< int, int >(nums, 0, [](int e, int acc) {return e + acc;});
   cout << "sum = " << sum << endl;

   int prod = Aggregate< int, int >(nums, 1, [](int e, int acc) {return e * acc;});
   cout << "prod = " << prod << endl;

   return 0;
}
</pre>
<pre class="prettyprint">
sum = 55
prod = 3628800
</pre>
<p>The first lambda function above sums the current element with the previous sum, which initially is given as 0. The result is 55. The second lambda function multiplies the current element with the previous product, which initially is 1. The result is 3628800.</p>
<p>But the Aggregate function can be used with other types too. Here is a last example with strings.</p>
<pre class="prettyprint">
int _tmain(int argc, _TCHAR* argv[])
{
   std::vector< string > words;
   words.push_back("the");
   words.push_back("quick");
   words.push_back("brown");
   words.push_back("fox");
   words.push_back("jumps");
   words.push_back("over");
   words.push_back("the");
   words.push_back("lazy");
   words.push_back("dog");

   string sentence = Aggregate< string, string >(
      words,
      "",
      [](string workingSentence, string next){return next + " " + workingSentence;});

   cout << sentence << endl;

   return 0;
}
</pre>
<pre class="prettyprint">
dog lazy the over jumps fox brown quick the
</pre>
<p>These were several examples of how lambda functions help us write more generic, and less verbose code. I suggest you read more about lambdas in C++ <a href="http://blogs.msdn.com/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0x-features-in-vc10-part-1.aspx" target="_blank">here</a>.</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/24/lambdas-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Optional and Named Parameters in C# 4.0</title>
		<link>http://mariusbancila.ro/blog/2009/02/23/optional-and-named-parameters-in-c-40/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/23/optional-and-named-parameters-in-c-40/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 10:00:18 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CTPs & Betas]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[named]]></category>
		<category><![CDATA[optional]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=170</guid>
		<description><![CDATA[So far, C#, unlike C++, did not support optional arguments. For instance, suppose you need a function to print log a message, that can add a new line or not after writing the message. Most of the times you want a new line, so you don&#8217;t want to specify that for most of the calls. [...]]]></description>
			<content:encoded><![CDATA[<p>So far, C#, unlike C++, did not support <i>optional arguments</i>. For instance, suppose you need a function to print log a message, that can add a new line or not after writing the message. Most of the times you want a new line, so you don&#8217;t want to specify that for most of the calls. Until now, the only possibility was using overloaded functions, with different parameters.</p>
<pre class="prettyprint">
class Logger
{
   public void LogMessage(string error, bool addNewLine)
   {
      Console.Write(error);
      if (addNewLine) Console.WriteLine();
   }

   public void LogMessage(string error)
   {
      LogMessage(error, true);
   }
};

class Program
{
   static void Main(string[] args)
   {
      Logger log = new Logger();
      log.LogMessage("trying to connect...", false);
      // do more stuff here
      log.LogMessage("SUCCESS");
   }
}
</pre>
<pre class="prettyprint">
trying to connect...SUCCESS
Press any key to continue . . .
</pre>
<p>C# 4.0, that will be released with Visual Studio 2010, and is for now available with the <a href="http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=922b4655-93d0-4476-bda4-94cf5f8d4814&#038;displaylang=en" target="_blank">Visual Studio 2010 CTP</a>, supports, just like C++, optional arguments. So instead of using overloaded functions, you can specify a default value for a parameter. When doing the call, if you don&#8217;t provide a value for the argument, the default one will be used.</p>
<p>The Logger class below is identical from the functionality point of view with the one above. (The previous Main() function doesn&#8217;t have to change.)</p>
<pre class="prettyprint">
class Logger
{
   public void LogMessage(string error, bool addNewLine = true)
   {
      Console.Write(error);
      if (addNewLine) Console.WriteLine();
   }
};
</pre>
<p>The only restriction is that optional parameters have to appear in the function after all required parameters. In other words, this is not legal:</p>
<pre class="prettyprint">
class foo
{
   public void func(int a, int b = 0, int c)
   {
   }
}
</pre>
<pre class="prettyprint">
error CS1737: Optional parameters must appear after all required parameters
</pre>
<p>Considering this implementation of foo</p>
<pre class="prettyprint">
class foo
{
   public void func(int a, int b = 0, int c = 1)
   {
   }
}
</pre>
<p>the following calls can be made:</p>
<pre class="prettyprint">
var f = new foo();
f.func(42, 3, 4);          // f.func(42, 3, 4)
f.func(42);                // f.func(42, 0, 1)
f.func(42, 100);           // f.func(42, 100, 1)
</pre>
<p>But this is not all. C# 4.0 brings another feature: <i>named parameters</i> (and this does not exist in C++). It means that when you make a call, you can specify an argument by its name, not by position. In this case you use the parameter&#8217;s name followed by &#8216;:&#8217; and the value. Here are several examples:</p>
<pre class="prettyprint">
var f = new foo();
f.func(42, c: 4);          // f.func(42, 0, 4)
f.func(42, c: 4, b: 3);    // f.func(42, 3, 4)
f.func(c: 3, a: 1, b: 2);  // f.func(1, 2, 3)
</pre>
<p>Of course, this can be used regardless the function has optional parameters or not. However, I would not use this feature for anything else than specifying an optional parameter when I would have to first suply values for other several optional parameters. In other words:</p>
<pre class="prettyprint">
var f = new foo();
f.func(42, c: 4);          // this is good, skip b, provide value for c
f.func(42, c: 4, b: 3);    // don't like this, rather call f.func(42, 3, 4)
f.func(c: 3, a: 1, b: 2);  // don't like this, rather call f.func(1, 2, 3)
</pre>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/23/optional-and-named-parameters-in-c-40/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type inference in C++</title>
		<link>http://mariusbancila.ro/blog/2009/02/22/type-inference-in-c/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/22/type-inference-in-c/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 02:48:19 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[CTPs & Betas]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[auto]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[decltype]]></category>
		<category><![CDATA[inference]]></category>
		<category><![CDATA[standard]]></category>
		<category><![CDATA[var]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=160</guid>
		<description><![CDATA[The new C++0x standard provides support for type inference. The auto keyword that was doing nothing in C++ was given a new meaning: a placeholder for a type inferred by the compiler. For those familiar with C#&#8217;s var keyword, this is basically the same. Here is a comparison between auto in C++ and var in [...]]]></description>
			<content:encoded><![CDATA[<p>The new C++0x standard provides support for type inference. The <font color="#0066CC"><b>auto</b></font> keyword that was doing nothing in C++ was given a new meaning: a placeholder for a type inferred by the compiler. For those familiar with C#&#8217;s <font color="#0066CC"><b>var</b></font> keyword, this is basically the same.</p>
<p>Here is a comparison between <font color="#0066CC"><b>auto</b></font> in C++ and <font color="#0066CC"><b>var</b></font> in C#.</p>
<table border="0" width="800px">
<tr valign="top">
<th width="400px">C++</th>
<th width="400px">C#</th>
</tr>
<tr valign="top">
<td width="400px">
<pre class="prettyprint">
int _tmain(int argc, _TCHAR* argv[])
{
   auto i = 42;
   auto s = "Hello world!";
   auto d = 12.50;
   auto l = [](int n) {return n * n;};

   cout << "i = " << i << endl;
   cout << "s = " << s << endl;
   cout << "d = " << d << endl;
   cout << "l(i) = " << l(i) << endl;

   return 0;
}
</pre>
</td>
<td width="400px">
<pre class="prettyprint">
class Program
{
   static void Main(string[] args)
   {
      var i = 42;
      var s = "hello world";
      var d = 12.50;
      //var l = (n) => n * n; // error CS0815

      Console.WriteLine("i = {0}", i);
      Console.WriteLine("s = {0}", s);
      Console.WriteLine("d = {0}", d);
   }
}
</pre>
</td>
</tr>
</table>
<p>with the output</p>
<table border="0" width="800px">
<tr valign="top">
<th width="400px">C++</th>
<th width="400px">C#</th>
</tr>
<tr valign="top">
<td width="400px">
<pre class="prettyprint">
i = 42
s = Hello world!
d = 12.5
l(i) = 1764
</pre>
</td>
<td width="50%">
<pre class="prettyprint">
i = 42
s = Hello world!
d = 12.5
</pre>
</td>
</tr>
</table>
<p>As you can see, the use is very similar, except that in C++ you can use auto for a lambda expression, while in C# you cannot. The C# compiler raises an error, CS0815, when you try to use var with an anonymous function expressions, a lambda expression, a method group expressions, or the null literal expression, because these don't have a type.</p>
<p>Just like with <font color="#0066CC"><b>var</b></font>, you can only use <font color="#0066CC"><b>auto</b></font> locally, and not as return type from a function, or parameter to a function.</p>
<pre class="prettyprint">
auto power(int n)
{
   return n * n;
}
</pre>
<pre class="prettyprint">
error C3532: 'auto (int)': incorrect usage of 'auto'
</pre>
<pre class="prettyprint">
int power(auto n)
{
   return n * n;
}
</pre>
<pre class="prettyprint">
error C3533: 'auto': a parameter cannot have a type that contains 'auto'
</pre>
<p>While it might not be of that much help for ints or strings, using auto for instead of vector&lt; int &gt;::iterator, or map&lt; string, list&lt; string &gt;&gt;::const_iterator comes in handy. The auto type inference helps us write less verbose code.</p>
<pre class="prettyprint">
vector< string > words;
words.push_back("hello");
words.push_back("world");

for(auto it = words.begin(); it != words.end(); ++it)
   cout << *it << endl;
</pre>
<p>In addition to <font color="#0066CC"><b>auto</b></font>, the C++0x standard introduces a new keyword, called <font color="#0066CC"><b>decltype</b></font> (think of it as 'typeof'), that can be used to determine the type of an expression at compile time.</p>
<pre class="prettyprint">
auto i = 42;
decltype(i) i2 = 44;
</pre>
<p>However, this new keyword was not yet introduced in Visual Studio 2010 CTP. According to <a href="http://blogs.msdn.com/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0x-features-in-vc10-part-1.aspx#9025183" target="_blank">Stephan T. Lavavej</a> from the VC++ team, it might be possible to be introduced in a next CTP or beta version.</p>
<blockquote><p>
Implementing auto doesn't implement decltype (C++0x's name for what was previously called typeof) for free.</p>
<p>Paraphrasing our libraries and compiler front-end program manager Damien Watkins, the CTP is only the first look at our VC10 functionality and there is definitely more to come. We understand that certain features "go together", and that adding complementary features is a good thing in general. As always, customer feedback is a vital resource in forming our plans.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/22/type-inference-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

