<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-880778854866581868</id><updated>2024-09-01T18:14:55.454+02:00</updated><category term="Pointers and Constants"/><category term="Argument Deduction"/><category term="Instantiation"/><category term="Nontype Template"/><category term="Overloading"/><category term="Pointers"/><category term="Variadic templates"/><category term="Algorithm"/><category term="Constructors"/><category term="Decltype"/><category term="Default Template"/><category term="Functional"/><category term="Introduction"/><category term="Lambdas"/><category term="Local Variables"/><category term="Matrices"/><category term="Pointers to Member Functions"/><category term="Specializations"/><category term="why templates"/><title type='text'>Surprising Code</title><subtitle type='html'>C++ and his secrets. A guide to improve your knowledge.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>25</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-3641471553732122782</id><published>2012-12-05T13:06:00.000+01:00</published><updated>2012-12-06T14:06:13.410+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="Functional"/><title type='text'>Using Algorithm Library and Functional</title><content type='html'>Just an example to show the utility of the library algorithm.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #3f3fbf;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #7f0055;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #3f3fbf;&quot;&gt;algorithm&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #7f0055;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #3f3fbf;&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #7f0055;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #3f3fbf;&quot;&gt;functional&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; op_increase (&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; i) { &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; ++i; }
&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; op_sum (&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; i, &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; j) { &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; i+j; }

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;void&lt;/span&gt; inc(&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt;&amp;amp; val){
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; i = 0;
    val += i;
    i += 10;
}

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;ostream&lt;/span&gt;&amp;amp; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;operator&lt;/span&gt;&amp;lt;&amp;lt;(&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;ostream&lt;/span&gt;&amp;amp; out, &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;vector&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt;&amp;gt;&amp;amp; a){
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; a.size(); i++)
        out &amp;lt;&amp;lt; a[i] &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;;
    
    out &amp;lt;&amp;lt; endl;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; out;
}
&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;main&lt;/span&gt; () {
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;vector&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt;&amp;gt; first(5);
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;vector&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt;&amp;gt; second;
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;vector&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt;&amp;gt; result;
  
  second.resize(first.size());    
  result.resize(first.size());

  &lt;span style=&quot;color: #3f7f59;&quot;&gt;// set some values:&lt;/span&gt;
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;for_each&lt;/span&gt;(first.begin(), first.end(), inc); 
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;for_each&lt;/span&gt;(second.begin(), second.end(), inc);
  
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; first &amp;lt;&amp;lt; second &amp;lt;&amp;lt; result;

  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;transform&lt;/span&gt; (first.begin(), first.end(), result.begin(), op_increase);
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; result;
  
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;transform&lt;/span&gt; (first.begin(), first.end(), second.begin(), result.begin(), op_sum);
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; result;

  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;vector&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt;&amp;gt; lenghts;
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;vector&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;string&lt;/span&gt;*&amp;gt; numbers;

  &lt;span style=&quot;color: #3f7f59;&quot;&gt;// populate vector of pointers:&lt;/span&gt;
  numbers.push_back ( &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;string&lt;/span&gt; (&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;one&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;) );
  numbers.push_back ( &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;string&lt;/span&gt; (&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;two&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;) );
  numbers.push_back ( &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;string&lt;/span&gt; (&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;three&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;) );
  numbers.push_back ( &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;string&lt;/span&gt; (&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;four&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;) );
  numbers.push_back ( &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;string&lt;/span&gt; (&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;five&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;) );

  lenghts.resize(numbers.size() );

  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;transform&lt;/span&gt; (numbers.begin(), numbers.end(), lenghts.begin(), &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;mem_fun&lt;/span&gt;(&amp;amp;&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;string&lt;/span&gt;::length) );
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; lenghts;

  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;system&lt;/span&gt;(&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;pause&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;);
  &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; 0;
}
&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;
&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;Also, it is possible to use mem_fun_ref instead of mem_fun without the need of use a vector of pointers to strings and just use strings instead of,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;font-family: Times New Roman;&quot;&gt;&lt;span style=&quot;white-space: normal;&quot;&gt;Thus, in conclusion, it&#39;s very interesting to know how to use this libraries because allow you to work more efficiently and do the same work in less time.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/3641471553732122782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/12/using-algorithm-library-and-functional.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3641471553732122782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3641471553732122782'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/12/using-algorithm-library-and-functional.html' title='Using Algorithm Library and Functional'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-1378219149964732794</id><published>2012-12-02T15:48:00.000+01:00</published><updated>2012-12-02T15:50:08.719+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Constructors"/><category scheme="http://www.blogger.com/atom/ns#" term="Local Variables"/><category scheme="http://www.blogger.com/atom/ns#" term="Pointers"/><category scheme="http://www.blogger.com/atom/ns#" term="Pointers and Constants"/><title type='text'>Pointers, References and Local Variables</title><content type='html'>Well, I bring here an example when you should figure out how constructors, pointers and reference works.&lt;br /&gt;
&lt;br /&gt;
Thus, here it goes:&lt;br /&gt;
&lt;br /&gt;
Note: Take a fast look to A class and start reading in main function.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #3f3fbf;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #7f0055;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #3f3fbf;&quot;&gt;algorithm&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #7f0055;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #7f0055;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #3f3fbf;&quot;&gt;stdlib.h&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;class&lt;/span&gt; A{
&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;private&lt;/span&gt;:
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; a;
&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;public&lt;/span&gt;:
    A() : a(-1){
        &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;Default constructor&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
    }
    A(&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; val) : a(val){
        &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;Calling contructor by int&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
    }
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;//************************************&lt;/span&gt;
    A(A* val){
        &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;Calling contructor by A_pointer&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
        a = val-&amp;gt;get();
    }
    A(A&amp;amp; val){
        &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;Calling contructor by A_reference&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
        a = val.get();
    }

    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// This can&#39;t be done because when the constructor is called&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// the parameter of the function calls itself, I mean, val is being&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// a local variable&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;/*A(A val){&lt;/span&gt;
&lt;span style=&quot;color: #3f7f59;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = val.get();&lt;/span&gt;
&lt;span style=&quot;color: #3f7f59;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}*/&lt;/span&gt;

    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; get(){ &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; a; }
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; get() &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;const&lt;/span&gt; { &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;get &#39;a&#39; by a const variable&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; a; }
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt;&amp;amp; getRef(){ &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; a; }
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;//int&amp;amp; getRef() const{ return a; } This can&#39;t be done&lt;/span&gt;
};


&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;void&lt;/span&gt; byPointers(A *a, A *b){
    *a = b-&amp;gt;get()*2; &lt;span style=&quot;color: #3f7f59;&quot;&gt;//(-1 * 2 = -2)&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// this can be done because a points to a instanced class A&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// in case that would point to null this would be a segmentacion fault&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// on runtime&lt;/span&gt;

    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// Beside, this calls the constructor by value&lt;/span&gt;
}

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;void&lt;/span&gt; byReference(A&amp;amp; a, &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;const&lt;/span&gt; A&amp;amp; b){
    a.getRef() = 1000; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// we change a_int value, because a is a reference and a.getRef return the atribut inside the class by reference&lt;/span&gt;
                       &lt;span style=&quot;color: #3f7f59;&quot;&gt;// if the int atribut would&#39;ve been public that would be equivalent to a.a = 1000;&lt;/span&gt;

    &lt;span style=&quot;color: #3f7f59;&quot;&gt;//so, because in this call, a and b are A_value, if we change a, also are changing b, let&#39;s look:&lt;/span&gt;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; b.get() &amp;lt;&amp;lt; endl;

    &lt;span style=&quot;color: #3f7f59;&quot;&gt;//However, we can&#39;t change the b value, because is constant although points to the same variable&lt;/span&gt;
}
&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;void&lt;/span&gt; byReference2(A&amp;amp; a){
    a.getRef() = 1000000;
}

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;void&lt;/span&gt; byCopy(A a){
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;a: &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; a.get() &amp;lt;&amp;lt; endl;

    a = A(25); 
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;a: &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; a.get() &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// now it&#39;s 25&lt;/span&gt;

    a.getRef() = 26;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;a: &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; a.get() &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// now it&#39;s 26 but still being&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// a local variable&lt;/span&gt;
}

&lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;First example:&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
    A A_value; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// this calls default constructor&lt;/span&gt;
    
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; A_value.get() &amp;lt;&amp;lt; endl;
    A* A_pointer; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// this doesn&#39;t because is a pointer&lt;/span&gt;
    A_pointer = &amp;amp;A_value; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// this doesn&#39;t call any A constructor&lt;/span&gt;
    *A_pointer = A_value; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// this is equivialent to the previous line&lt;/span&gt;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; A_pointer-&amp;gt;get() &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; (*A_pointer).get() &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #3f7f59;&quot;&gt;// point to A_value (two ways to access it)&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;//**************************************************************************************&lt;/span&gt;

    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;Second example:&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
    byPointers(&amp;amp;A_value,&amp;amp;A_value); &lt;span style=&quot;color: #3f7f59;&quot;&gt;// this doesn&#39;t call any constructor&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// just points to the references of A_value, so inside the function &lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// if we change A *a, A *b pointers we&#39;ll change A_value&lt;/span&gt;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; endl;

    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// This is a bit obfuscated, so peruse&lt;/span&gt;
    byCopy(A_value); &lt;span style=&quot;color: #3f7f59;&quot;&gt;// byCopy expect to creat (A a) as a local variable&lt;/span&gt;
                     &lt;span style=&quot;color: #3f7f59;&quot;&gt;// so calls A_reference, or method to copy a class&lt;/span&gt;
                     &lt;span style=&quot;color: #3f7f59;&quot;&gt;// however, the construtor is by reference, a still be&lt;/span&gt;
                     &lt;span style=&quot;color: #3f7f59;&quot;&gt;// a local variable&lt;/span&gt;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; A_value.get() &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;span style=&quot;color: #3f7f59;&quot;&gt;// Thus, we haven&#39;t changed any attribut of A_value&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;//**************************************************************************************&lt;/span&gt;
    
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;Third example:&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
    byReference(A_value, A_value); &lt;span style=&quot;color: #3f7f59;&quot;&gt;// pass by reference doesn&#39;t call any constructor&lt;/span&gt;
                                   &lt;span style=&quot;color: #3f7f59;&quot;&gt;// because a and b are exactly A_value&lt;/span&gt;
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// So, after the call, A_value have changed:&lt;/span&gt;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; A_value.get() &amp;lt;&amp;lt; endl;

    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// even, we can do the same with the pointer&lt;/span&gt;
    byReference2(*A_pointer);
    &lt;span style=&quot;color: #3f7f59;&quot;&gt;// and because A_pointer points to A_value, this will also change the value&lt;/span&gt;
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;cout&lt;/span&gt; &amp;lt;&amp;lt; A_value.get() &amp;lt;&amp;lt; endl; 

    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;system&lt;/span&gt;(&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;pause&lt;/span&gt;&lt;span style=&quot;color: #2a00ff;&quot;&gt;&quot;&lt;/span&gt;);
    &lt;span style=&quot;color: #7f0055; font-weight: bold;&quot;&gt;return&lt;/span&gt; 0;
}
&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/1378219149964732794/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/12/pointers-references-and-local-variables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1378219149964732794'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1378219149964732794'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/12/pointers-references-and-local-variables.html' title='Pointers, References and Local Variables'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-706722545246215307</id><published>2012-11-30T14:32:00.000+01:00</published><updated>2012-11-30T14:32:29.291+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Matrices"/><category scheme="http://www.blogger.com/atom/ns#" term="Pointers"/><title type='text'>Example of two and three dimensional pointers </title><content type='html'>Welcome everyone,&lt;br /&gt;
I did a little problem to practice with pointers to other pointers. In this case, I did a matrix and pass it by a function than expect the reference of it.&lt;br /&gt;
&lt;br /&gt;
The example compute the multiplication of two matrices.&lt;br /&gt;
&lt;br /&gt;
Note: This post was&amp;nbsp;inspired answering that post:&amp;nbsp;http://www.cplusplus.com/forum/general/86553/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Enjoy it:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;algorithm&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdlib.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; value&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; val &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    val&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    value &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; val&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; zero&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; value&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    value &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;


&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; mult&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;A&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;B&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; j &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; j &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; j&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; k &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; k &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; k&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
                &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; B&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;A&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: dimgrey;&quot;&gt;//reserve memory:&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;malloc&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;for_each&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;//fill&lt;/span&gt;

    &lt;span style=&quot;color: dimgrey;&quot;&gt;//print&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: dimgrey;&quot;&gt;//reserve memory for a pair of matrices:&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;matrix&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    matrix &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;malloc&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;malloc&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;result&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    result &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;malloc&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;malloc&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//***************************&lt;/span&gt;

    &lt;span style=&quot;color: dimgrey;&quot;&gt;//fill matrix:&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;for_each&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;  &lt;span style=&quot;color: #603000;&quot;&gt;for_each&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;zero&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;  
    &lt;span style=&quot;color: #603000;&quot;&gt;for_each&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;  &lt;span style=&quot;color: #603000;&quot;&gt;for_each&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;zero&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;for_each&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;  &lt;span style=&quot;color: #603000;&quot;&gt;for_each&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;zero&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: dimgrey;&quot;&gt;//print it&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; j &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; j &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; j&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; matrix&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    mult&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;matrix&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;result&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//print it&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; j &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; j &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; j&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; result&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: #603000;&quot;&gt;system&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;pause&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/706722545246215307/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/11/example-of-two-and-three-dimensional.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/706722545246215307'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/706722545246215307'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/11/example-of-two-and-three-dimensional.html' title='Example of two and three dimensional pointers '/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-7116874235541357113</id><published>2012-11-25T00:59:00.000+01:00</published><updated>2012-11-25T01:00:35.404+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Decltype"/><title type='text'>Type Traits 2</title><content type='html'>Have I ever spoken about decltpe?&lt;br /&gt;
Well, I just mentioned.&lt;br /&gt;
&lt;br /&gt;
In a previous post, I spoke about&amp;nbsp;&lt;a href=&quot;http://www.surprising-code.com/2012/03/type-traits.html&quot; style=&quot;background-color: white; color: #4d469c; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 14px; line-height: 10.666666984558105px; text-decoration: initial; text-indent: -15px;&quot;&gt;Type Traits&lt;/a&gt;. Now, I&#39;m going further and show you some details about decltype; a new feature of C++11.&lt;br /&gt;
&lt;br /&gt;
This is useful because you will spend less time having to write out things the compiler already knows.&lt;br /&gt;
C++11&amp;nbsp;introduces&amp;nbsp;several tools for type inference. Decltype, auto, new return feature, these are some examples.&lt;br /&gt;
&lt;br /&gt;
In this chapter, I&#39;ll review references and&amp;nbsp;introduce decltype.&lt;br /&gt;
Read carefully and note the types.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; a &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;b&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;a&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; c &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; a&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;//int referenced&lt;/span&gt;
    c &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//This modifies a&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;
&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;auto&lt;/span&gt; d &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// that&#39;s an int, isn&#39;t referenced&lt;/span&gt;
    d &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; e &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; a&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// that&#39;s a referenced int&lt;/span&gt;
    e &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; a &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; c &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;b &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; d &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// all refers to the same number&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//*****************************************************&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; q1 &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;q1&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; q2 &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;q1&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; q3 &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; q1&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// int&amp;amp;&lt;/span&gt;
    q3 &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; q1 &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; q2 &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; q3 &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//*****************************************************&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;struct&lt;/span&gt; MyClass &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;double&lt;/span&gt; x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;const&lt;/span&gt; MyClass&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;const&lt;/span&gt; f &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;new&lt;/span&gt; MyClass&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// constant pointer pointing to constant value&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;    decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt; f &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; asd &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;new&lt;/span&gt; MyClass&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// const MyClass* const so have to be initialized&lt;/span&gt;
    decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt; f&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;x &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// double&lt;/span&gt;
    decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;f&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; y &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// const double&amp;amp;&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: purple;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;Notice that decltype( (variable) ) &amp;nbsp;with double parenthesis is referenced.&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;Now, I want to introduce a new way to declare function.&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;div style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;
The idea:&lt;/div&gt;
&lt;pre&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;typename&lt;/span&gt; Builder&lt;span style=&quot;color: purple;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;auto&lt;/span&gt;
makeAndProcessObject &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;const&lt;/span&gt; Builder&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; builder&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt; decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt; builder&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;makeObject&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;auto&lt;/span&gt; val &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; builder&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;makeObject&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// do anything&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; val&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;div style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;
A short example:&lt;/div&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;utility&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;class&lt;/span&gt; A&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;typedef&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;pair&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;&amp;gt;&lt;/span&gt; T&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;static&lt;/span&gt; T build&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; T&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;ostream&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; printA&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;ostream&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; out&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;const&lt;/span&gt; T&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; val&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        out &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;( &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; val&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;first &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; val&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;second &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; )&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; out&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// we can override the operator &amp;lt;&amp;lt;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// inside the class&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// friend ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; out, const A&amp;amp; val)&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// but we don&#39;t want to print an A class, we just are printing an&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// internal type&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: dimgrey;&quot;&gt;// NEW C++11&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;auto&lt;/span&gt; foo&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt; decltype&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt; A&lt;span style=&quot;color: purple;&quot;&gt;::&lt;/span&gt;build&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;auto&lt;/span&gt; a &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; A&lt;span style=&quot;color: purple;&quot;&gt;::&lt;/span&gt;build&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// DO ANITHING&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; a&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #603000;&quot;&gt;ostream&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;ostream&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; out&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;const&lt;/span&gt; A&lt;span style=&quot;color: purple;&quot;&gt;::&lt;/span&gt;T&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; val&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; A&lt;span style=&quot;color: purple;&quot;&gt;::&lt;/span&gt;printA&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;out&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; val&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// overloading &amp;lt;&amp;lt; operator for cout &amp;lt;&amp;lt; A::T&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; foo&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt; endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// If we change A::T class and build constructor, foo() will change automaticaly the return type&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/7116874235541357113/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/11/type-traits-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7116874235541357113'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7116874235541357113'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/11/type-traits-2.html' title='Type Traits 2'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-6530337747273826471</id><published>2012-08-30T19:40:00.001+02:00</published><updated>2012-08-30T19:40:41.989+02:00</updated><title type='text'>A-star Search</title><content type='html'>Hello Folks,&lt;br /&gt;
Today I won&#39;t write code but instead of I&#39;ll speak about a famous algorithm to find optimal&amp;nbsp;paths.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;A* search&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
It evaluates nodes by combining &lt;b&gt;g(n) &lt;/b&gt;and &lt;b&gt;h(n)&lt;/b&gt;, where &lt;b&gt;g(n)&lt;/b&gt; is the cost to reach the current node and &lt;b&gt;h(n)&lt;/b&gt; is the heuristic cost from the note to the goal.&lt;br /&gt;
&lt;br /&gt;
So, by combining both we have &lt;b&gt;f(n)&lt;/b&gt; that is the estimated cost of the cheapest solution.&lt;br /&gt;
This algorithm is both complete and optimal whether satisfies certain heuristic conditions.&lt;br /&gt;
In fact, the algorithm is identical to &lt;b&gt;Uniform-Cost-Search&lt;/b&gt;, also known by the name &lt;b&gt;Dijkstra&lt;/b&gt;, except that uses &lt;b&gt;g(n) + h(n)&lt;/b&gt; instead of &lt;b&gt;g(n)&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;Conditions for optimality&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
The main condition we require for optimality is that &lt;b&gt;h(n)&lt;/b&gt; has to be an&amp;nbsp;&lt;b&gt;admissible&amp;nbsp;heuristic&lt;/b&gt;. An admissible heuristic is one that never overestimates the cost to reach the goal.&lt;br /&gt;
&lt;br /&gt;
In other words, g(n) is the minimal&amp;nbsp;accumulated&amp;nbsp;cost from the&amp;nbsp;initial&amp;nbsp;state to the current node and h(n) is heuristic cost from the current node to the goal. Thus, f(n) that is g(n) + h(n) is the cost from the initial state to the goal.&lt;br /&gt;
So, admissibility here means that h(n) has to be calculated heuristicly and this means that won&#39;t be the real cost.&lt;br /&gt;
&lt;br /&gt;
So, if h(n) is bigger that h*(n), which is the real cost or the perfect heuristic, then f(n) = g(n) + h(n) would be bigger than the shortest path and due to wouldn&#39;t be optimal.&lt;br /&gt;
&lt;br /&gt;
An example of an admissible heuristic is the straight-line distance h_SLD. Whether we want to calculate the shortest path between two cities h_SLD will be an&amp;nbsp;excellent&amp;nbsp;heuristic to be used because between two points always the shortest path is a straight-line and don&#39;t exist a better solution.&amp;nbsp;Thus, this heuristic will be admissible because would never calculate a further path than the optimal solution.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;A*: Additional Properties&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
A* store in memory all nodes visited so his complexity is&amp;nbsp;exponential&amp;nbsp;both time and space.&lt;br /&gt;
Thus, A* is not practical for many large-scale problems. There are, algorithms, that overcome the space problem without sacrificing optimality or completeness, at a small cost in execution time.&lt;br /&gt;
For instance, IDA*.&lt;br /&gt;
&lt;br /&gt;
A* is optimal in another sense: no other algorithm expands less nodes than A* with same heuristic function but this doesn&#39;t mean that A* is always the fastest.&lt;br /&gt;
Another property is that better heuristic functions A* expands less nodes.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;0 &amp;lt; h_1 &amp;lt; h_2 &amp;lt; h*&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
With h_2 will expand less nodes than with h_1.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Example of heuristics&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
I won&#39;t explain any of it but just with the name you will be able to google it.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Manhattan distance in N-puzzle&lt;/li&gt;
&lt;li&gt;Euclidean Distance in Routing Finding&lt;/li&gt;
&lt;li&gt;Spanning Tree in Travelling Salesman Problem&lt;/li&gt;
&lt;li&gt;Shortest Path in Job Shop Scheduling&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Algorithm&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://upload.wikimedia.org/wikipedia/commons/5/5d/Astar_progress_animation.gif&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://upload.wikimedia.org/wikipedia/commons/5/5d/Astar_progress_animation.gif&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
Illustration of A* search for finding path from a start node to a goal node.&lt;br /&gt;
&lt;br /&gt;
Extracted from &lt;a href=&quot;http://en.wikipedia.org/wiki/A*_search_algorithm&quot;&gt;wikipedia&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://upload.wikimedia.org/wikipedia/commons/5/5d/AstarExample.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;227&quot; src=&quot;http://upload.wikimedia.org/wikipedia/commons/5/5d/AstarExample.gif&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
In order to solve the shortest path in the graph using A* I will use two list, one for the current nodes ( search frontier ) and another for the visited nodes ( in order to check loops ).&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Opened&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;u&gt;1&lt;/u&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;u&gt;2&lt;/u&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;u&gt;3&lt;/u&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;and so on&lt;/i&gt;&lt;br /&gt;
&lt;u&gt;f(a) = 1.5 + 4 = 5.5&lt;/u&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;u&gt;f(b) = 3.5 + 2 = 5.5&lt;/u&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; f(c) = 6.5 + 2 = 8.5&lt;br /&gt;
f(d) = 2 + 4.5 = 6.5 &amp;nbsp; &amp;nbsp; &amp;nbsp; f(d) = 2 + 4.5 = 6.5 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;u&gt;f(d) = 2 + 4.5 = 6.5&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Closed&lt;/u&gt;&lt;br /&gt;
&lt;i&gt;&lt;u&gt;1&lt;/u&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;u&gt;2&lt;/u&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;u&gt;3&lt;/u&gt;&lt;/i&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; f(a) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;f(a)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;f(b)&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/6530337747273826471/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/08/a-star-search.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/6530337747273826471'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/6530337747273826471'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/08/a-star-search.html' title='A-star Search'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-2782399587593951821</id><published>2012-05-19T04:32:00.000+02:00</published><updated>2012-06-30T23:49:29.290+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Nontype Template"/><title type='text'>Random vector</title><content type='html'>&lt;span style=&quot;background-color: white;&quot;&gt;Last post it was about lambdas but, indeed, I just focused with an utility; random vectors creation. Now, after still being researching I found more intuitive and elegant forms.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Let&#39;s see this amazing things:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;algorithm&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;time.h&amp;gt;

&lt;span style=&quot;color: green;&quot;&gt;//Start reading inside main!!!!&lt;/span&gt;
&lt;span style=&quot;color: green;&quot;&gt;//first template is the rank of numbers we want&lt;/span&gt;
&lt;span style=&quot;color: green;&quot;&gt;//second, where it starts&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; N, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; Start = 0&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; MyRand{
&lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;operator&lt;/span&gt;()(&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;){
         &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; std::rand()%N + Start;
     }
};

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; fRand(){
     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; std::rand()%10 + 30;
}

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main()
{
      &lt;span style=&quot;color: green;&quot;&gt;// Create a vector object that contains 10 elements.&lt;/span&gt;
      std::vector&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; a(10);

     &lt;span style=&quot;color: green;&quot;&gt;// randomly initialize using lambda expressions&lt;/span&gt;
     srand((&lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt;)time(0)); 

     &lt;span style=&quot;color: green;&quot;&gt;//generating the vector by means of standard random function&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//This is nice but you don&#39;t have much control of which rank of numbers&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//will be returned. Indeed, you know, with the macro RAND_MAX&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//and this is a huge number.&lt;/span&gt;

     &lt;span style=&quot;color: green;&quot;&gt;//And that perhaps isn&#39;t what we expected. So the solucion is to use&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//a custom function:&lt;/span&gt;
     std::generate(a.begin(), a.end(), std::rand );

     &lt;span style=&quot;color: green;&quot;&gt;//Now, just changing fRand we can get our wished numbers, but&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//even that is not the best way because you can&#39;t use any function&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//that takes some parameter and then you&#39;d need as many function&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//as you need; and that can be tedious. Instead of this, we can use templates&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//which we won&#39;t get that problem.&lt;/span&gt;
     std::generate(a.begin(), a.end(), fRand );

     &lt;span style=&quot;color: green;&quot;&gt;//So, here we go:&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//With two templated parameters we can get any random rank of numberes we want.&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//for example, if we want numbers between 10 and 20 that&#39;s the example:&lt;/span&gt;
     std::generate(a.begin(), a.end(), MyRand&amp;lt;10,10&amp;gt;() );

     &lt;span style=&quot;color: green;&quot;&gt;//O maybe we want numbers between 14 and 71&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//how many numbers there are between them? 71 - 14 = 57 and we start at 14&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//So:&lt;/span&gt;
     std::generate(a.begin(), a.end(), MyRand&amp;lt;57,14&amp;gt;() );


     &lt;span style=&quot;color: green;&quot;&gt;//like last time, how to write this vector with just code a line&lt;/span&gt;
     std::for_each(a.begin(), a.end(), [](&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; a){ std::cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; std::endl; } );
     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;code&gt;
&lt;/code&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/2782399587593951821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/05/random-vector.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/2782399587593951821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/2782399587593951821'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/05/random-vector.html' title='Random vector'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-8361476643018452310</id><published>2012-05-13T00:01:00.001+02:00</published><updated>2012-05-21T16:18:19.770+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Lambdas"/><title type='text'>Lambdas</title><content type='html'>Yes, I&#39;m still alive hehe&lt;br /&gt;
Today, I have been working with C++11 and I did some awesome things.&lt;br /&gt;
&lt;br /&gt;
In this post, I won&#39;t explain much, just because I&#39;m still inexperienced but I will.&lt;br /&gt;
&lt;br /&gt;
How many times did you print a vector? And how many lines was needed?&amp;nbsp;Yes, of course. You could make a function but so many times you are just debuging and you aren&#39;t really interested with it because you are solving anothers problems of mayor importance.&lt;br /&gt;
&lt;br /&gt;
Here, there&#39;s an example with one line to do it. Moreover, I&amp;nbsp;randomly&amp;nbsp;initialized the vector with the same idea.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;algorithm&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;time.h&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main()
{
    &lt;span style=&quot;color: green;&quot;&gt;// Create a vector object that contains 10 elements.&lt;/span&gt;
    vector&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; a(10);

    &lt;span style=&quot;color: green;&quot;&gt;// randomly initialize using lambda expressions&lt;/span&gt;
    srand((&lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt;)time(0)); 
    for_each(a.begin(), a.end(), [](&lt;span style=&quot;color: green;&quot;&gt;/*by reference*/&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;amp; aux){&lt;/pre&gt;
&lt;pre&gt;        aux = rand()%100; } );

    &lt;span style=&quot;color: green;&quot;&gt;//now, print it&lt;/span&gt;
    for_each(a.begin(), a.end(), [](&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; aux){ cout &amp;lt;&amp;lt; aux &amp;lt;&amp;lt; endl; } );

    &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are not used to use for_each, here is an example without use lambdas:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;algorithm&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; printThing(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; i){
     cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;
}

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(){
    &lt;span style=&quot;color: green;&quot;&gt;// Create a vector object that contains 10 elements.&lt;/span&gt;
    vector&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; a(10);

    &lt;span style=&quot;color: green;&quot;&gt;// randomly initialize using lambda expressions&lt;/span&gt;
    srand((&lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt;)time(0)); 
    for_each(a.begin(), a.end(), [](&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;amp; aux){aux = rand()%100; } );

    for_each(a.begin(), a.end(), printThing );
    &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
And another example using templates, just getting fun:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;algorithm&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; printThing(T i){ &lt;span style=&quot;color: green;&quot;&gt;//void printThing(const T&amp;amp; i) would be better/faster&lt;/span&gt;
     cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;
}

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(){
    &lt;span style=&quot;color: green;&quot;&gt;// Create a vector object that contains 10 elements.&lt;/span&gt;
    vector&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; a(10);

    &lt;span style=&quot;color: green;&quot;&gt;// randomly initialize using lambda expressions&lt;/span&gt;
    srand((&lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt;)time(0)); 
    for_each(a.begin(), a.end(), [](&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;amp; aux){aux = rand()%100; } );

    for_each(a.begin(), a.end(), printThing&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;&amp;gt; );&lt;span style=&quot;color: green;&quot;&gt;//yes is char,&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: green;&quot;&gt;    //just to show that we are casting&lt;/span&gt;
    &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/8361476643018452310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/05/lambdas.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/8361476643018452310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/8361476643018452310'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/05/lambdas.html' title='Lambdas'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-7360039938867670623</id><published>2012-03-29T13:24:00.004+02:00</published><updated>2012-03-29T13:24:44.223+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Variadic templates"/><title type='text'>Playing with variadic</title><content type='html'>Okey, here&#39;s an example with variadics. Of course, that&#39;s not ever the best way, in fact, it&#39;s absolutely bad&amp;nbsp;practice but I was just playing with.&lt;br /&gt;
&lt;br /&gt;
So, if you are interested in what this guy did? go ahead if not and you want to learn and even use&lt;br /&gt;
some of my ideas, don&#39;t try it.&lt;br /&gt;
&lt;br /&gt;
Being weighed, this is not a good example, just take it for fun.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt;...Types&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; List{
&lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; size;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;** elements;&lt;span style=&quot;color: green;&quot;&gt;//aray of things&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; List(){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; size = &lt;span style=&quot;color: blue;&quot;&gt;sizeof&lt;/span&gt;...(Types);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; List(Types... Args){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; size = &lt;span style=&quot;color: blue;&quot;&gt;sizeof&lt;/span&gt;...(Types);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elements = &lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;*[size];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(size != &lt;span style=&quot;color: blue;&quot;&gt;sizeof&lt;/span&gt;...(Args) )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;diferent size&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;else&lt;/span&gt;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;good&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; push(0,Args...);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt;:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; T&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; push(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; pos, T last){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt;(pos == size-1){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;at: &quot;&lt;/span&gt; &amp;lt;&amp;lt; pos &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot; added -&amp;gt; &quot;&lt;/span&gt; &amp;lt;&amp;lt; *last &amp;lt;&amp;lt; std::endl;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;All correct&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elements[pos] = &lt;span style=&quot;color: blue;&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;*&amp;gt;(last);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; First, &lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt;...Rest&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; push(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; pos, First first, Rest...rest){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elements[pos] = &lt;span style=&quot;color: blue;&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;*&amp;gt;(first);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;at: &quot;&lt;/span&gt; &amp;lt;&amp;lt; pos &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot; added -&amp;gt; &quot;&lt;/span&gt; &amp;lt;&amp;lt; *first &amp;lt;&amp;lt; std::endl;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pos++;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; push(pos,rest...);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
};

&lt;span style=&quot;color: blue;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; A{
&lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string info;
&lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A(string in) : info(in){};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;friend&lt;/span&gt; ostream&amp;amp; &lt;span style=&quot;color: blue;&quot;&gt;operator&lt;/span&gt;&amp;lt;&amp;lt;(ostream&amp;amp; out, &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; A&amp;amp; a){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out &amp;lt;&amp;lt; a.info;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; out;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };
};
&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;*,&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;*,&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;*,string*,A*&amp;gt; list(&lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;(1),&lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; (2),&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;                     new&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt; (2.101), &lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt; string(&lt;span style=&quot;color: red;&quot;&gt;&quot;aloha&quot;&lt;/span&gt;),&lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt; A(&lt;span style=&quot;color: red;&quot;&gt;&quot;NICE&quot;&lt;/span&gt;) );

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;size: &quot;&lt;/span&gt; &amp;lt;&amp;lt; list.size &amp;lt;&amp;lt; endl;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; *&lt;span style=&quot;color: blue;&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;*&amp;gt;(list.elements[0]) &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; *&lt;span style=&quot;color: blue;&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;*&amp;gt;(list.elements[1]) &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; *&lt;span style=&quot;color: blue;&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;*&amp;gt;(list.elements[2]) &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; *&lt;span style=&quot;color: blue;&quot;&gt;static_cast&lt;/span&gt;&amp;lt;string*&amp;gt;(list.elements[3]) &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; *&lt;span style=&quot;color: blue;&quot;&gt;static_cast&lt;/span&gt;&amp;lt;A*&amp;gt;(list.elements[4]) &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
&lt;/fieldset&gt;
&lt;br /&gt;
Output:
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
good&lt;br /&gt;
at: 0 added -&amp;gt; 1&lt;br /&gt;
at: 1 added -&amp;gt; 2&lt;br /&gt;
at: 2 added -&amp;gt; 2.101&lt;br /&gt;
at: 3 added -&amp;gt; aloha&lt;br /&gt;
at: 4 added -&amp;gt; NICE&lt;br /&gt;
All correct&lt;br /&gt;
size: 5&lt;br /&gt;
1, 2, 2.101, aloha, NICE,&lt;br /&gt;
&lt;br /&gt;&lt;/fieldset&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/7360039938867670623/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/03/playing-with-variadic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7360039938867670623'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7360039938867670623'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/03/playing-with-variadic.html' title='Playing with variadic'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-4397172255073446479</id><published>2012-03-28T23:51:00.000+02:00</published><updated>2012-03-28T23:51:19.331+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Variadic templates"/><title type='text'>Variadic templates</title><content type='html'>Every time, every time, C++ surprise me. Today, with variadic templates that allow us to specifier an unknown number of arguments.&amp;nbsp;And this becomes an completely useful tool.&lt;br /&gt;
&lt;br /&gt;
At the moment, I won&#39;t explain it because still being a mystery for me. Just say that this allow&amp;nbsp;recursion&lt;br /&gt;
like prolog. Moreover, we can make containers of different types and even very things that I don&#39;t know.&lt;br /&gt;
&lt;br /&gt;
Here&#39;s my example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: green;&quot;&gt;//base case:&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; printArray(T last){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; last &amp;lt;&amp;lt; endl;
}

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; First, &lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt;...Rest&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; printArray(First first, Rest... rest){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; first &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(rest...);
}

&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; A{
&lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string info;
&lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A(string in) : info(in){};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;friend&lt;/span&gt; ostream&amp;amp; &lt;span style=&quot;color: blue;&quot;&gt;operator&lt;/span&gt;&amp;lt;&amp;lt;(ostream&amp;amp; out, &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; A&amp;amp; a){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out &amp;lt;&amp;lt; a.info;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; out;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };
};

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt;){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(1);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(1,2);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(1,2,3);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(1,2,3,4,5);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(&lt;span style=&quot;color: red;&quot;&gt;&quot;I&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(&lt;span style=&quot;color: red;&quot;&gt;&quot;I&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;can&#39;t&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(&lt;span style=&quot;color: red;&quot;&gt;&quot;I&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;can&#39;t&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;that&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(&lt;span style=&quot;color: red;&quot;&gt;&quot;I&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;can&#39;t&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;that&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;this&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(&lt;span style=&quot;color: red;&quot;&gt;&quot;I&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;can&#39;t&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;that&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;this&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;works&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: green;&quot;&gt;//diferent types, even a custom class&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printArray(1,&lt;span style=&quot;color: red;&quot;&gt;&quot;aloha&quot;&lt;/span&gt;,2.123,&#39;a&#39;, A(&lt;span style=&quot;color: red;&quot;&gt;&quot;Com estas?&quot;&lt;/span&gt;) );
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
&lt;/fieldset&gt;
&lt;br /&gt;
&lt;b&gt;Output:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
1, 2, 3&lt;br /&gt;
1, 2, 3, 4, 5&lt;br /&gt;
I&lt;br /&gt;
I, can&#39;t&lt;br /&gt;
I, can&#39;t, that&lt;br /&gt;
I, can&#39;t, that, this&lt;br /&gt;
I, can&#39;t, that, this, works&lt;br /&gt;
1, aloha, 2, que tal?&lt;br /&gt;
1, aloha, 2.123, a, Com estas?&lt;br /&gt;
&lt;div style=&quot;font-weight: bold;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/fieldset&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/4397172255073446479/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/03/variadic-templates.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/4397172255073446479'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/4397172255073446479'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/03/variadic-templates.html' title='Variadic templates'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-4861014648866457449</id><published>2012-03-28T21:21:00.000+02:00</published><updated>2012-03-28T21:23:13.958+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Argument Deduction"/><title type='text'>String arguments in templates</title><content type='html'>Well, well, well.&lt;br /&gt;
Up to this point, you should have a minimal idea of templates. Of course, there are some chapter&lt;br /&gt;
that you didn&#39;t understand yet or you didn&#39;t take the whole idea that I was trying to teach.&lt;br /&gt;
&lt;br /&gt;
But don&#39;t worry, you just need reviews like this post to understand that some of the titles I made&lt;br /&gt;
are of huge interest and even, indeed, more complex.&lt;br /&gt;
&lt;br /&gt;
Here there&#39;s an example that show that the chapter, argument deduction, is more complex as it&lt;br /&gt;
seems and we don&#39;t have to take it as if nothing because even with the string type we can get&lt;br /&gt;
much mistakes as you think.&lt;br /&gt;
&lt;br /&gt;&lt;fieldset&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;string&amp;gt;
&lt;span style=&quot;color: green;&quot;&gt;// note: reference parameters&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; T&amp;amp; max (&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; T&amp;amp; b){
     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; a &amp;lt; b ? b : a;
}
&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(){
     std::string s;
     ::max(&lt;span style=&quot;color: red;&quot;&gt;&quot;bye&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;cya&quot;&lt;/span&gt;); &lt;span style=&quot;color: green;&quot;&gt;// OK: same type&lt;/span&gt;
     ::max(&lt;span style=&quot;color: red;&quot;&gt;&quot;hello&quot;&lt;/span&gt;,&lt;span style=&quot;color: red;&quot;&gt;&quot;bye&quot;&lt;/span&gt;); &lt;span style=&quot;color: green;&quot;&gt;// ERROR: different types&lt;/span&gt;
     ::max(&lt;span style=&quot;color: red;&quot;&gt;&quot;chau&quot;&lt;/span&gt;,s); &lt;span style=&quot;color: green;&quot;&gt;// ERROR: different types&lt;/span&gt;
}&lt;/pre&gt;&lt;/fieldset&gt;
&lt;br /&gt;
&lt;br /&gt;
First look, you may think, what the hell is happening here, how it doesn&#39;t work?, but if you take long&lt;br /&gt;
time and read the compiler you&#39;ll that is an error about types.&lt;br /&gt;
&lt;br /&gt;
First off, remember that string is a C++ type and when you declare some thing like &quot;this (with commas)&quot; you are not really creating an string if not that is an char*. Thus, max(), is not&lt;br /&gt;
calling the function max with string&#39;s type if not that are char&#39;s.&lt;br /&gt;
&lt;br /&gt;
Beside, we have to notice, in order to see the error, that the problem becomes from pass arguments&lt;br /&gt;
as reference (&amp;amp;) because without this we didn&#39;t get error from first and second call.&lt;br /&gt;
That&#39;s it because without pass by reference the type pass it is const char*. On the other hand, calling&lt;br /&gt;
by reference, we are passing the following type. char const[3] where this number is the size of&lt;br /&gt;
the string.&lt;br /&gt;
&lt;br /&gt;
This makes that two strings of different sizes makes an error like in the second call where&lt;br /&gt;
&quot;hello&quot; is 4 and &quot;bye&quot; is 3(insist, all this is by argument deduction).&lt;br /&gt;
&lt;br /&gt;
There are many forms to solve this problem like be sure that strings are strings(for instance, string(&quot;bye&quot;) ) but I want to handle it with templates; it&#39;s not as elegant as I&#39;d like but this is it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;fieldset&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; N, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; M&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* max (&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; T (&amp;amp;a)[N], &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; T (&amp;amp;b)[M]){
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; a &amp;lt; b ? b : a;
}&lt;/pre&gt;&lt;/fieldset&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/4861014648866457449/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/03/string-arguments-in-templates.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/4861014648866457449'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/4861014648866457449'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/03/string-arguments-in-templates.html' title='String arguments in templates'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-7079716235805322407</id><published>2012-03-23T16:43:00.000+01:00</published><updated>2012-03-28T19:56:11.805+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Argument Deduction"/><category scheme="http://www.blogger.com/atom/ns#" term="Specializations"/><title type='text'>Type Traits</title><content type='html'>Hello everybody,&lt;br /&gt;
Lately I have been snowed under work, in fact, I still. But I&#39;m posting anyway ^ ^&lt;br /&gt;
&lt;br /&gt;
Well, here&#39;s an&amp;nbsp;introduction&amp;nbsp;to some features&amp;nbsp;of templates type traits.&lt;br /&gt;
&lt;br /&gt;
Notice that this post is just an introduction and if you don&#39;t see the utility now you will see in&amp;nbsp;further&amp;nbsp;posts. Just say that generic programming can sometimes be not generic enough.&lt;br /&gt;
&lt;br /&gt;
Starting to take a look to the standard way to check types of variables.&lt;br /&gt;
&lt;br /&gt;
The standard provide some ways to do it. Although has his limitations and even isn&#39;t much effective but&lt;br /&gt;
allows to compare or even read the type of variables.&lt;br /&gt;
&lt;br /&gt;
Doing this task I&#39;m using typeid that return a std::type_info where is implemented some&lt;br /&gt;
methods like name in order to return a human-readable.&lt;br /&gt;
&lt;br /&gt;
I don&#39;t know why my compiles expres int as i, I think that is not much normal because I have seen&lt;br /&gt;
many examples where typeid().name return the word int, not i, but in fact don&#39;t care.&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;code&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;pre class=&quot;”brush:&quot; js”=&quot;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: green;&quot;&gt;// type_info example&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;typeinfo&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; Base {};
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; Derived : Base {};

&lt;span style=&quot;color: green;&quot;&gt;//using virtual; being polymorphic&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; Poly_Base {&lt;span style=&quot;color: blue;&quot;&gt;virtual&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; Member(){}};
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; Poly_Derived: Poly_Base {};

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main() {
     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; i;
     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; *p_int = 0;
     &lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt; array_d[10];

     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;    int is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;      i is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(i).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;  int==i? : &quot;&lt;/span&gt; &amp;lt;&amp;lt; boolalpha &amp;lt;&amp;lt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;           (&lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(i) == &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;)) &amp;lt;&amp;lt; endl;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: green;&quot;&gt;            //with boolapha we&#39;ll print true/false inside of 1/0&lt;/span&gt;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;  p_int is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(p_int).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot; *p_int is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(*p_int).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;array_d is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(array_d).name() &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;
   
     &lt;span style=&quot;color: green;&quot;&gt;// non-polymorphic types:&lt;/span&gt;
     Derived derived;
     Base* pbase = &amp;amp;derived;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;derived is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(derived).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;  pBase is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(pbase).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot; *pbase is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(*pbase).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot; same type? &quot;&lt;/span&gt;; 
     cout &amp;lt;&amp;lt; boolalpha &amp;lt;&amp;lt;( &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(derived)==&lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(*pbase) )&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;          &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;

     &lt;span style=&quot;color: green;&quot;&gt;// polymorphic types:&lt;/span&gt;
     Poly_Derived polyderived;
     Poly_Base* ppolybase = &amp;amp;polyderived;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;polyderived is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(polyderived).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;  ppolybase is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(ppolybase).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot; *ppolybase is: &quot;&lt;/span&gt; &amp;lt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(*ppolybase).name() &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; boolalpha &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;same type? &quot;&lt;/span&gt; &amp;lt;&amp;lt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;           ( &lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(polyderived)==&lt;span style=&quot;color: blue;&quot;&gt;typeid&lt;/span&gt;(*ppolybase) ) &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;
}&lt;/code&gt;&lt;/pre&gt;
&lt;code&gt;
&lt;/code&gt;
&lt;/fieldset&gt;
&lt;br /&gt;
&lt;b&gt;Output:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; int==i? : true&lt;br /&gt;
&amp;nbsp; &amp;nbsp; p_int is : Pi&lt;br /&gt;
&amp;nbsp; *p_int is : i&lt;br /&gt;
array_d is : A10_d&lt;br /&gt;
&lt;br /&gt;
derived is : 7Derived&lt;br /&gt;
&amp;nbsp; pBase is : P4Base&lt;br /&gt;
&amp;nbsp;*pbase is : 4Base&lt;br /&gt;
&amp;nbsp;same type? false&lt;br /&gt;
&lt;br /&gt;
polyderived is : 12Poly_Derived&lt;br /&gt;
&amp;nbsp; &amp;nbsp;ppolybase is : P9Poly_Base&lt;br /&gt;
&amp;nbsp;*ppolybase is : 12Poly_Derived&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; same type? true&lt;/fieldset&gt;
&lt;br /&gt;
Useful? Could be.&lt;br /&gt;
Although, as I said, it has some limitations, for example, what happens if I want to compare an&lt;br /&gt;
usigned int and an int, is it equal? The answer is no; false.&lt;br /&gt;
&lt;br /&gt;
So, now, I want to show how we could do this task using temples. The behind idea is argument deduction and&amp;nbsp;specialization.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
&lt;code&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; is_int{
     &lt;span style=&quot;color: blue;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;bool&lt;/span&gt; value = &lt;span style=&quot;color: blue;&quot;&gt;false&lt;/span&gt;;
};
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; is_int&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt;{
     &lt;span style=&quot;color: blue;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;bool&lt;/span&gt; value = &lt;span style=&quot;color: blue;&quot;&gt;true&lt;/span&gt;;
};
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; is_int&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt;{
     &lt;span style=&quot;color: blue;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;bool&lt;/span&gt; value = &lt;span style=&quot;color: blue;&quot;&gt;true&lt;/span&gt;;
};
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; is_int&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;&amp;gt;{
     &lt;span style=&quot;color: blue;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;bool&lt;/span&gt; value = &lt;span style=&quot;color: blue;&quot;&gt;true&lt;/span&gt;;
};

&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; A{};

&lt;span style=&quot;color: blue;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main() {
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;is int?: &quot;&lt;/span&gt; &amp;lt;&amp;lt; boolalpha &amp;lt;&amp;lt; is_int&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt;::value &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;is unsigned int?: &quot;&lt;/span&gt; &amp;lt;&amp;lt; boolalpha&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;          &amp;lt;&amp;lt;&amp;nbsp;is_int&lt;/code&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt;::value &amp;lt;&amp;lt; endl;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;is string?: &quot;&lt;/span&gt; &amp;lt;&amp;lt; boolalpha &amp;lt;&amp;lt; is_int&amp;lt;string&amp;gt;::value &amp;lt;&amp;lt; endl;
     cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;is A?: &quot;&lt;/span&gt; &amp;lt;&amp;lt; boolalpha &amp;lt;&amp;lt; is_int&amp;lt;A&amp;gt;::value &amp;lt;&amp;lt; endl;
}&lt;/code&gt;&lt;/pre&gt;
&lt;code&gt;
&lt;/code&gt;&lt;/fieldset&gt;
&lt;br /&gt;
&lt;b&gt;Output:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;fieldset&gt;
is int?: true&lt;br /&gt;
is unsigned int?: true&lt;br /&gt;
is string?: false&lt;br /&gt;
is A?: false&lt;/fieldset&gt;
&lt;br /&gt;
Finally, just say that&amp;nbsp;&lt;a href=&quot;http://boost.org/&quot;&gt;boost library&lt;/a&gt;&amp;nbsp;provide many of this template functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;boost/type_traits&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
And even functions like:&lt;br /&gt;
&lt;br /&gt;
is_pointer&amp;lt;T&amp;gt;&lt;br /&gt;
is_arithmetic&amp;lt;T&amp;gt;&lt;br /&gt;
is_function&amp;lt;T&amp;gt;&lt;br /&gt;
is_class&amp;lt;T&amp;gt;&lt;br /&gt;
is_polymorphic&amp;lt;T&amp;gt;&lt;br /&gt;
has_trivial_constructor&amp;lt;T&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Awesome, I know.
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Also, I wanted to mention to the new&amp;nbsp;&lt;b style=&quot;background-color: white; font-family: DejaVuSansMono, &#39;DejaVu Sans Mono&#39;, courier, monospace; font-size: 14px; font-weight: bold; line-height: 15px;&quot;&gt;decltype&amp;nbsp;&lt;/b&gt;that is introduced in the new version of C++,&lt;br /&gt;
this one will be a tricky form to declare variables that you don&#39;t know how, because his complexity&lt;br /&gt;
can be complex like templates. It&#39;s not exactly&amp;nbsp;related with what I explained but I think that&lt;br /&gt;
may be interesting to someone.</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/7079716235805322407/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/03/type-traits.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7079716235805322407'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7079716235805322407'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/03/type-traits.html' title='Type Traits'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-132774827842649981</id><published>2012-03-05T15:17:00.001+01:00</published><updated>2012-03-25T14:18:56.150+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Pointers and Constants"/><title type='text'>Pointers and Constants using Int</title><content type='html'>Hello everybody,&lt;br /&gt;
&lt;br /&gt;
I did a short example to give an expression of pointers and constants. It&#39;s uniquely illustrative. With all this, I&#39;m trying to lay the groundwork to const correctness&#39;s use because the good use of const is absolutely important in complex&amp;nbsp;applications&amp;nbsp;where the&amp;nbsp;efficient&amp;nbsp;matter.&lt;br /&gt;
&lt;br /&gt;
Here we go:&lt;br /&gt;
&lt;fieldset&gt;
&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;
&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(){
           &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;         intInt;
     &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;         cint = 10;&lt;span style=&quot;color: green;&quot;&gt;//we have to initialize here unless&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: green;&quot;&gt;                                 that is an atribut&#39;s class&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;                                 &lt;span style=&quot;color: green;&quot;&gt;//and, in this case, have to be&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: green;&quot;&gt;                                 initialize inside the constructor&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;           &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; *       ptrInt;
     &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; *       constValue;
           &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; * &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; constPtr = &amp;amp;intInt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;//have to be initialize here&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;
     &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; * &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt; &lt;/span&gt;&lt;/code&gt;&lt;span style=&quot;color: #333333;&quot;&gt;constPtrValue = &amp;amp;intInt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;//have to be initialize&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;                                               here&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;     
     &lt;span style=&quot;color: green;&quot;&gt;//intInt&lt;/span&gt;
     intInt = 1;
     intInt = 2;
     &lt;span style=&quot;color: green;&quot;&gt;//we can change the value as times as we like&lt;/span&gt;
     
     &lt;span style=&quot;color: green;&quot;&gt;//cint&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//cint = 2; EROR: this variable is const so is the same&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;     to say read-only&lt;/span&gt;
     
     &lt;span style=&quot;color: green;&quot;&gt;//ptrInt&lt;/span&gt;
     ptrInt = &amp;amp;intInt;
     &lt;span style=&quot;color: green;&quot;&gt;//ptrInt = &amp;amp;cint; ERROR: the reference from cint is &#39;const int*&#39;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;     and was expected int*&lt;/span&gt;
     *ptrInt = cint; &lt;span style=&quot;color: green;&quot;&gt;//we can put the value of cint inside our pointer&lt;/span&gt;
                     &lt;span style=&quot;color: green;&quot;&gt;//but in this case we have copied by value and no&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;                     by reference so&lt;/span&gt;
                     &lt;span style=&quot;color: green;&quot;&gt;//*ptrInt don&#39;t point to a const and then&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;                     we can increment his value:&lt;/span&gt;
     (*ptrInt)++;    &lt;span style=&quot;color: green;&quot;&gt;//now is 11&lt;/span&gt;
     
     &lt;span style=&quot;color: green;&quot;&gt;//constant pointer&lt;/span&gt;
     constValue = &amp;amp;cint; &lt;span style=&quot;color: green;&quot;&gt;//we can point to any constant int&lt;/span&gt;
     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; aux = 20;
     constValue = &amp;amp;aux; &lt;span style=&quot;color: green;&quot;&gt;//even to a int but still being unaccesible&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;                        using constValue&lt;/span&gt;
     intInt = 30;
     &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; *auxPtr = &amp;amp;intInt;
     constVconstValue = auxPtr;&lt;span style=&quot;color: green;&quot;&gt;//in this way the value of constValue is 30&lt;/span&gt;
                         &lt;span style=&quot;color: green;&quot;&gt;//and still being unaccesible from constValue&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//*constValue = 12; ERROR: the value of constValue is const,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;     so *constValue is read-only&lt;/span&gt;
     intInt = 100;       &lt;span style=&quot;color: green;&quot;&gt;//but we can change the value from the variable&lt;/span&gt;
                         &lt;span style=&quot;color: green;&quot;&gt;//so this is legal, and then&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; *constValue;&lt;span style=&quot;color: green;&quot;&gt;//this print 100&lt;/span&gt;
     
     (*auxPtr)++;        &lt;span style=&quot;color: green;&quot;&gt;//but we still having access also from our auxPtr&lt;/span&gt;
                         &lt;span style=&quot;color: green;&quot;&gt;//so this is legal to&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; *constValue;&lt;span style=&quot;color: green;&quot;&gt;//and now, this print 101&lt;/span&gt;
     
     &lt;span style=&quot;color: green;&quot;&gt;//int * const constPtr&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//constPtr = auxPtr; EROR: our pointer is constant so we can&#39;t change&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;     the direction where point&lt;/span&gt;
     *constPtr = 2;&lt;span style=&quot;color: green;&quot;&gt;//but the value isn&#39;t constant, so we can change it&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; *constPtr; &lt;span style=&quot;color: green;&quot;&gt;// is 2&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; intInt;&lt;span style=&quot;color: green;&quot;&gt;// and the value have been changed too, is 2&lt;/span&gt;
     
     &lt;span style=&quot;color: green;&quot;&gt;//const int * const constPtrValue&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; *constPtrValue;&lt;span style=&quot;color: green;&quot;&gt;// is 2 because we are pointing to intInt&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//constPtrValue = ptrInt; ERROR: assigment of read-only variable&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//*constPtrValue = 20; ERROR: read-only value&lt;/span&gt;
                 &lt;span style=&quot;color: green;&quot;&gt;//so we only have access to the variable&lt;/span&gt;
                 &lt;span style=&quot;color: green;&quot;&gt;//although we can change the value with other variables&lt;/span&gt;
     intInt = 666;
     std::cout &amp;lt;&amp;lt; *constPtrValue; &lt;span style=&quot;color: green;&quot;&gt;//this print 666&lt;/span&gt;
}&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
&lt;code&gt;
&lt;/code&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/fieldset&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/132774827842649981/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/03/pointers-and-constant-using-int.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/132774827842649981'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/132774827842649981'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/03/pointers-and-constant-using-int.html' title='Pointers and Constants using Int'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-3887939131941483495</id><published>2012-02-29T02:20:00.000+01:00</published><updated>2012-03-03T23:02:18.991+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Pointers to Member Functions"/><title type='text'>Pointers to Functions</title><content type='html'>&lt;span style=&quot;font-size: large;&quot;&gt;First FAQ post&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Check it out!!&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://surprising-code.blogspot.com/p/pointers-to-member-functions.html&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;http://surprising-code.blogspot.com/p/pointers-to-member-functions.html&lt;/span&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/3887939131941483495/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/pointers-to-member-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3887939131941483495'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3887939131941483495'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/pointers-to-member-functions.html' title='Pointers to Functions'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-1988171584528766160</id><published>2012-02-28T17:28:00.001+01:00</published><updated>2012-09-05T13:40:02.284+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Nontype Template"/><title type='text'>Nontype Template Parameters</title><content type='html'>&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Konnichiwa!!&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Lately I have been very excited programming and I really have to do an effort to continue chapter after chapter in order not to mess up the blog. However, if you are interested to get further feel free to check out the examples page.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;So then, let&#39;s work!&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;First off, what&#39;s the meaning of nontype template parameters? Indeed, the idea is so simple, is just to use types like int inside templates. Till now, all template examples we have seen has been with the reserve word typename or class but the powerful of templates don&#39;t finish here.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;For example, a Matrix:&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; W, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; H&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;struct&lt;/span&gt; Matrix{
    &lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt; m[W][H];
};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Thus, now, we can simply constructor of a Matrix like this:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Matrix&amp;lt;4,4&amp;gt; A={0,0,0,0,&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;               0,1,0,0,&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;               0,0,1,0,&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;               0,0,0,1};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;pre&gt;&lt;code&gt;Matrix&amp;lt;4,1&amp;gt; B={1,2,3,4};&lt;/code&gt;&lt;/pre&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;In the case using a class in lieu of a struct, we could not do it in this way unless we started using C++11.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Another example usefull with matrices could be the below:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; sizeAX,&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; sizeAY, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; sizeBY&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; mult(&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; Matrix&amp;lt;sizeAX,sizeAY&amp;gt;&amp;amp; A,&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; Matrix&amp;lt;sizeAY,sizeBY&amp;gt;&amp;amp; B,&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Matrix&amp;lt;sizeAX,sizeBY&amp;gt;&amp;amp; C);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Note: I&#39;m using pass by reference, check out &lt;a href=&quot;http://surprising-code.blogspot.com/p/faq.html&quot;&gt;FAQ&lt;/a&gt; if dont get it at all why I used &amp;amp;&#39;s or const.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;This function multipy does A · B = C&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;But not always matrices can be multiplied. With the definition we did, we can do A · B but not B · A.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;In this case we&#39;ll have a compile error:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;(I change the variable names to make it more understandable)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; mult(&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; Matrix&amp;lt;4,1&amp;gt;&amp;amp; A,&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; Matrix&amp;lt;4,4&amp;gt;&amp;amp; B,&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; mult(&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; Matrix&amp;lt;sizeAX,sizeAY&amp;gt;&amp;amp; A,&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; Matrix&amp;lt;sizeAY,sizeBY&amp;gt;&amp;amp; B,&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sizeAX = 4; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sizeAY = 4;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sizeAY = 1; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sizeBY = 4;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;SizeAY don&#39;t match so here is where the error occurs. But, in fact, is better to have this error in compile time than runtime.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Struct Matrix &lt;a href=&quot;http://surprising-code.blogspot.com/p/struct-matrix.html&quot;&gt;here&lt;/a&gt;!!&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;A similar example, &lt;a href=&quot;http://surprising-code.blogspot.com/p/matrix-templated.html&quot;&gt;here&lt;/a&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; ROWS, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; COLS&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; Matrix;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; line-height: 18px;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; InCOLS&amp;gt;
Matrix&amp;lt;T, ROWS, InCOLS&amp;gt; &lt;span style=&quot;color: blue;&quot;&gt;operator&lt;/span&gt;*(&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; Matrix&amp;lt;T, COLS, InCOLS&amp;gt;&amp;amp; in) &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: &#39;Helvetica Neue&#39;, Arial, Helvetica, sans-serif;&quot;&gt;If template COLS of our matrix match with the rows of the supose matrix to be multiply then we are able to do it. And the result of this multiply will be ROWS and InCOLS.

Slowly:&lt;/span&gt;&lt;span style=&quot;color: #444444;&quot;&gt;

&lt;/span&gt;&lt;span style=&quot;color: #444444; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;A(3,2)*B(2*10) = C(3,10)&lt;/span&gt;&lt;span style=&quot;color: #444444;&quot;&gt;

&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Helvetica Neue&#39;, Arial, Helvetica, sans-serif;&quot;&gt;If colsA(2) is equal to rowsB(2).

sizeC = (rowsA, colsB)&lt;/span&gt;&lt;span style=&quot;color: #444444;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;div style=&quot;color: #444444;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #444444; font-size: 13px;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: #444444; font-size: 13px; line-height: 18px;&quot;&gt;&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/1988171584528766160/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/nontype-template-parameters.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1988171584528766160'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1988171584528766160'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/nontype-template-parameters.html' title='Nontype Template Parameters'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-8326934848572808088</id><published>2012-02-27T21:49:00.000+01:00</published><updated>2012-02-27T21:49:52.302+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="why templates"/><title type='text'>Summary</title><content type='html'>A short summary.&lt;br /&gt;
&lt;br /&gt;
So far, I have introduced templates programming step by step with some examples. I didn&#39;t explain many things by myself, I tried to show you how it works.&lt;br /&gt;
&lt;br /&gt;
Bearing all of this in mind, it&#39;s time to go over the profits of templates and the reason of being so interesant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Templates are type-safe: &lt;/b&gt;The compiler is able to do the type checking before a error occurs.&lt;/li&gt;
&lt;/ul&gt;
If you write general code for a common base class you lose this benefits. Moreover &amp;nbsp;is expected you may derive this class, which makes it even more difficult to maintain.&lt;br /&gt;&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Easiness writing them: &lt;/b&gt;With just one class we may have an infinity number of uses&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Remember the example of max, with which one we saved many definitions.&amp;nbsp;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Optimize the runtime by compiling time:&amp;nbsp;&lt;/b&gt;This point, I&#39;d like to flesh out through metaprogramming but stay with the idea.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Good possibility to reuse code.&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/8326934848572808088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/summary.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/8326934848572808088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/8326934848572808088'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/summary.html' title='Summary'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-728649995079359086</id><published>2012-02-26T18:39:00.000+01:00</published><updated>2012-02-27T13:36:06.786+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Default Template"/><title type='text'>Default Template Arguments</title><content type='html'>Hello everybody, sorry for the delay. I have been so busy although I posted some examples.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Today, I&#39;ll show you an useful tool with templates. You will able to define default templates, this, it is so interested especially for containers.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
Some chapters before we defined a &lt;a href=&quot;http://surprising-code.blogspot.com/p/example7.html&quot;&gt;Queue class&lt;/a&gt;. Now, we retake it.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;How it looks a Default Template:&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; CONT = std::vector&amp;lt;T&amp;gt; &amp;gt;
&lt;br /&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
T, like previous examples, is the type that contain our class. And CONT is the template parameter that the class will use as container.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
Using the new class Queue ( at the end you the sheet or &lt;a href=&quot;http://surprising-code.blogspot.com/p/example9.html&quot;&gt;here&lt;/a&gt; ):&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
Queue&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; queueInt;&lt;br /&gt;
Queue&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;, std::deque&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;gt; queueIntWithDeque;&lt;br /&gt;
&lt;br /&gt;
So, we can indicate the container that our queue will use or not. Taking the dafault argument in this case.&lt;br /&gt;
&lt;br /&gt;
Even, we can define a default type, for example:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt;&amp;nbsp;T = &lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt;&amp;nbsp;CONT = std::vector&amp;lt;T&amp;gt; &amp;gt;&lt;br /&gt;
&lt;br /&gt;
And now, we can use the default templates, for instance:&lt;br /&gt;
&lt;br /&gt;
Queue&amp;lt;&amp;gt; defaultExample;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example which I will explain deeper in the next chapter are the nontype template parameters.&lt;br /&gt;
Basically, we are also able to define these as default.&lt;br /&gt;
&lt;br /&gt;
A short example:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;nbsp;H =&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;2&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;int&amp;nbsp;&lt;/span&gt;W = 2 &amp;nbsp;&amp;gt; (example of nontype template parameters &lt;a href=&quot;http://surprising-code.blogspot.com/p/matrix-templated.html&quot;&gt;here&lt;/a&gt;)&lt;br /&gt;
&lt;br /&gt;
Okey, let&#39;s see the new class Queue:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;stdexcept&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;deque&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T = &lt;span style=&quot;color: blue;&quot;&gt;double&lt;/span&gt;, &lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; CONT = std::vector&amp;lt;T&amp;gt; &amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; Queue{
      &lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt;:
          CONT elements;
          
      &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
          Queue(){}&lt;span style=&quot;color: green;&quot;&gt;//default constructor&lt;/span&gt;
          T back(){ &lt;span style=&quot;color: green;&quot;&gt;// return last element&lt;/span&gt;
              &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; elements.back();
          }
          &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; push(&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; T&amp;amp; element){&lt;span style=&quot;color: green;&quot;&gt;//add an element&lt;/span&gt;
               elements.push_back(element);
          }
          &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; pop(){&lt;span style=&quot;color: green;&quot;&gt;//erase first element&lt;/span&gt;
              &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (elements.empty()) {
                  &lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; std::out_of_range(&lt;span style=&quot;color: red;&quot;&gt;&quot;Stack&amp;lt;&amp;gt;::pop(): empty stack&quot;&lt;/span&gt;);
              }
              elements.erase( elements.begin() );
          }
          &lt;span style=&quot;color: blue;&quot;&gt;bool&lt;/span&gt; empty(){&lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; elements.empty();}
          &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; size(){&lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; elements.size();
          }
          T front(){
              &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (elements.empty()) {
                  &lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; std::out_of_range(&lt;span style=&quot;color: red;&quot;&gt;&quot;Stack&amp;lt;&amp;gt;::pop(): empty stack&quot;&lt;/span&gt;);
              }
              &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; elements.front();}
};
          
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; iterate_Queue(Queue&amp;lt;T&amp;gt;&amp;amp; queue){
      &lt;span style=&quot;color: blue;&quot;&gt;while&lt;/span&gt;(not queue.empty()) {
          std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;(&quot;&lt;/span&gt; &amp;lt;&amp;lt; queue.front()&amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt; &amp;lt;&amp;lt; queue.back() &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;)&quot;&lt;/span&gt;;
          std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;with size:&quot;&lt;/span&gt;&amp;lt;&amp;lt; queue.size() &amp;lt;&amp;lt; std::endl;
          queue.pop();
      }
      std::cout &amp;lt;&amp;lt; std::endl;
}
&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: green;&quot;&gt;// overloading function&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; CONT&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; iterate_Queue(Queue&amp;lt;T,CONT&amp;gt;&amp;amp; queue){
      &lt;span style=&quot;color: blue;&quot;&gt;while&lt;/span&gt;(not queue.empty()) {
          std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;(&quot;&lt;/span&gt; &amp;lt;&amp;lt; queue.front()&amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt; &amp;lt;&amp;lt; queue.back() &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;)&quot;&lt;/span&gt;;
          std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;with size:&quot;&lt;/span&gt;&amp;lt;&amp;lt; queue.size() &amp;lt;&amp;lt; std::endl;
          queue.pop();
      }
      std::cout &amp;lt;&amp;lt; std::endl;
}


&lt;span style=&quot;color: blue;&quot;&gt;typedef&lt;/span&gt; Queue&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;Queue;&lt;span style=&quot;color: green;&quot;&gt;// It&#39;s common to define typedef&#39;s in order to simply &lt;/span&gt;

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main() {
      &lt;span style=&quot;color: blue;&quot;&gt;try&lt;/span&gt;{
          &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;Queue y;
          y.push(1);
          y.push(2);
          y.push(3);
          y.push(4);
          iterate_Queue(y);

          y.push(15);
          iterate_Queue(y);

          
          Queue&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;, std::deque&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;gt; newExample;
         &amp;nbsp;newExample.push(10);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;newExample.push(20);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;newExample.push(30);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;newExample.push(40);&lt;/pre&gt;
&lt;pre&gt;          iterate_Queue(newExample);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;newExample.push(150);&lt;/pre&gt;
&lt;pre&gt;          iterate_Queue(newExample);&lt;/pre&gt;
&lt;pre&gt;          std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;complet dafault example &quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
          Queue&amp;lt;&amp;gt; defaultExample;
         &amp;nbsp;defaultExample.push(1.3);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;defaultExample.push(2.2);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;defaultExample.push(3.1);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;defaultExample.push(4.4);&lt;/pre&gt;
&lt;pre&gt;          iterate_Queue(defaultExample);&lt;/pre&gt;
&lt;pre&gt;         &amp;nbsp;defaultExample.push(150);&lt;/pre&gt;
&lt;pre&gt;          iterate_Queue(defaultExample);&lt;/pre&gt;
&lt;pre&gt;          
      } &lt;span style=&quot;color: blue;&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; std::exception&amp;amp; ex) {
          std::cerr &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;Exception: &quot;&lt;/span&gt; &amp;lt;&amp;lt; ex.what() &amp;lt;&amp;lt; std::endl;
      } 
      
      &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;output:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(1, 4)with size:4&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(2, 4)with size:3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(3, 4)with size:2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(4, 4)with size:1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(15, 15)with size:1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(10, 40)with size:4&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(20, 40)with size:3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(30, 40)with size:2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(40, 40)with size:1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(150, 150)with size:1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;complet dafault example&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(1.3, 4.4)with size:4&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(2.2, 4.4)with size:3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(3.1, 4.4)with size:2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(4.4, 4.4)with size:1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;(150, 150)with size:1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #666666;&quot;&gt;RUN SUCCESSFUL (total time: 615ms)&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
However, default templates have their own shortcomings, for example, in this case, we did the Queue methods using functions like: back(), front(), ...&lt;br /&gt;
&lt;br /&gt;
All this methods has to be implemented in the container we are going to use. In the case we didn&#39;t, we&#39;ll get some errors in time compiling (fortunately).&lt;br /&gt;
&lt;br /&gt;
Templates are so useful for this, you can check errors in compiling time. If you saw the example of a Matrix class that I did, you can see this feature. We can multiply matrixes, and at compiling time, we can be sure all the products are possible.&lt;br /&gt;
&lt;br /&gt;
Enjoy it!</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/728649995079359086/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/default-template-arguments.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/728649995079359086'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/728649995079359086'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/default-template-arguments.html' title='Default Template Arguments'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-9050240334160638209</id><published>2012-02-21T11:38:00.002+01:00</published><updated>2012-02-21T11:38:37.527+01:00</updated><title type='text'>Specializations</title><content type='html'>Morning!&lt;br /&gt;
&lt;br /&gt;
Today, I want to show the specialize of classes. Thereby, you can give a diferent implementation.&lt;br /&gt;
&lt;br /&gt;
Templates are so useful but sometimes could be necessary to make exceptions. C++ provide this tool that is similar to override functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Testing specialitzations:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;

&lt;span style=&quot;color: green;&quot;&gt;//base case or primary template&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T1, &lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T2&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; Example{
     &lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt;:
         T1 _element1;
         T2 _element2;
     &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
         Example(){ 
             std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;Example&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
         }
};

&lt;span style=&quot;color: green;&quot;&gt;//Specializations or also called partial or full specialization&lt;/span&gt;
&lt;span style=&quot;color: green;&quot;&gt;//depending on each case&lt;/span&gt;

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; Example&amp;lt;T, T&amp;gt;{
     &lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt;:
         &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; _element1;
         &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; _element2;
     &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
         Example(){ 
             std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;Example same class&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
         }
};


&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; Example&amp;lt;T, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt;{
     &lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt;:
         &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; _element1;
         &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; _element2;
     &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
         Example(){ 
             std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;Example template and int&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
         }
};

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T1,&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T2&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; Example&amp;lt;T1*,T2*&amp;gt;{
     &lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt;:
         &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; _element1;
         &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; _element2;
     &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
         Example(){ 
             std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;Example template* and template* &quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
         }
};

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(){
     Example&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;,&lt;span style=&quot;color: blue;&quot;&gt;float&lt;/span&gt;&amp;gt; ex1;
     Example&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;float&lt;/span&gt;,&lt;span style=&quot;color: blue;&quot;&gt;float&lt;/span&gt;&amp;gt; ex2;
     Example&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;float&lt;/span&gt;,&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; ex3;
     Example&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;*,&lt;span style=&quot;color: blue;&quot;&gt;float&lt;/span&gt;*&amp;gt; ex4;
     &lt;span style=&quot;color: green;&quot;&gt;//Example&lt;int,int&gt; ex5; // error:&lt;/int,int&gt;&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;/*
      * &lt;span style=&quot;color: red;&quot;&gt;main.cpp:57:22: error: ambiguous class template instantiation for ‘class Example&amp;lt;int, int&amp;gt;’&lt;/span&gt;
      * &lt;span style=&quot;color: red;&quot;&gt;main.cpp:19:20: error: candidates are: class Example&amp;lt;T, T&amp;gt;&lt;/span&gt;
      * &lt;span style=&quot;color: red;&quot;&gt;main.cpp:31:22: error:                 class Example&amp;lt;T, int&amp;gt;&lt;/span&gt;
      * 
      * To fix it we might specify:
      * 
      * template &amp;lt;&amp;gt;
      * class Example&amp;lt;int,int&amp;gt;
      */&lt;/span&gt;
     
     &lt;span style=&quot;color: green;&quot;&gt;//Example&lt;int*,int*&gt; ex6; // error:&lt;/int*,int*&gt;&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;/*
      * &lt;span style=&quot;color: red;&quot;&gt;main.cpp:64:24: error: ambiguous class template instantiation for ‘class Example&amp;lt;int*, int*&amp;gt;’&lt;/span&gt;
      * &lt;span style=&quot;color: red;&quot;&gt;main.cpp:19:20: error: candidates are: class Example&amp;lt;T, T&amp;gt;&lt;/span&gt;
      * &lt;span style=&quot;color: red;&quot;&gt;main.cpp:42:23: error:                 class Example&amp;lt;T1*, T2*&amp;gt;&lt;/span&gt;
      * 
      * To solve this issue we could implement a specialitzacion for pointers
      * of the same type:
      * 
      * template&amp;lt;typename T&amp;gt;
      * Example&amp;lt;T*, T*&amp;gt;
      * 
      */&lt;/span&gt;
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/9050240334160638209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/specializations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/9050240334160638209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/9050240334160638209'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/specializations.html' title='Specializations'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-3842203246315742217</id><published>2012-02-19T23:38:00.000+01:00</published><updated>2012-02-20T18:55:44.619+01:00</updated><title type='text'>First template class, queue</title><content type='html'>Hello again!&lt;br /&gt;
&lt;br /&gt;
I hope you have started to see the useful of templates, till now, I only showed templates with functions but, of course, that was just the start.&lt;br /&gt;
&lt;br /&gt;
Now, I&#39;ll begin showing template classes. One of the most famous examples to show this are container classes, specially Stack. To change, I have done a Queue.&lt;br /&gt;
&lt;br /&gt;
By the way, I have programmed using the exception&amp;nbsp;library, if you don&#39;t get it, just ignored and think you could implement with other ways.&lt;br /&gt;
This way programming is so common in Java and if you come from it should be so easy to you.&lt;br /&gt;
&lt;br /&gt;
First of all, what is the behave of a queue? just think with the queue of a cinema. First in, first out.&lt;br /&gt;
Example of the behave having this queue: [ a, b, c, d ]&lt;br /&gt;
&lt;br /&gt;
With the back function we get d.&lt;br /&gt;
With a push(e) we&#39;ll have&amp;nbsp;[ a, b, c, d, e ]&lt;br /&gt;
With pop():&amp;nbsp;[ b, c, d, e ]&lt;br /&gt;
empty(): false&lt;br /&gt;
size(): 4&lt;br /&gt;
front(): b&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Well, here you have the code Note that I used a STL container internally.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;stdexcept&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; Queue{
     &lt;span style=&quot;color: blue;&quot;&gt;private&lt;/span&gt;:
         std::vector&amp;lt;T&amp;gt; _elements;
         
     &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt;:
         Queue(){}&lt;span style=&quot;color: green;&quot;&gt;//default constructor&lt;/span&gt;
         T back(){ &lt;span style=&quot;color: green;&quot;&gt;// return last element&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;         if&lt;/span&gt; (_elements.empty()) {
                 &lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; std::out_of_range(&lt;span style=&quot;color: red;&quot;&gt;&quot;Queue&amp;lt;&amp;gt;::back(): empty stack&quot;&lt;/span&gt;);
             }&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; _elements.back();
         }
         &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; push(&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; T&amp;amp; element){&lt;span style=&quot;color: green;&quot;&gt;//add an element&lt;/span&gt;
              _elements.push_back(element);
         }
         &lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; pop(){&lt;span style=&quot;color: green;&quot;&gt;//erase first element&lt;/span&gt;
             &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (_elements.empty()) {
                 &lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; std::out_of_range(&lt;span style=&quot;color: red;&quot;&gt;&quot;Queue&amp;lt;&amp;gt;::pop(): empty stack&quot;&lt;/span&gt;);
             }
             _elements.erase( _elements.begin() );
         }
         &lt;span style=&quot;color: blue;&quot;&gt;bool&lt;/span&gt; empty(){&lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; _elements.empty();}
         &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; size(){&lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; _elements.size();}
         T front(){
             &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (_elements.empty()) {
                 &lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; std::out_of_range(&lt;span style=&quot;color: red;&quot;&gt;&quot;Queue&amp;lt;&amp;gt;::front(): empty stack&quot;&lt;/span&gt;);
             }
             &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; _elements.front();}
};
         
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt;&amp;lt; &lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; T &amp;gt;
&lt;span style=&quot;color: green;&quot;&gt;//Note that we are passing by reference, so original queue will be erased&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;void&lt;/span&gt; iterate_Queue(Queue&amp;lt;T&amp;gt;&amp;amp; queue){
     &lt;span style=&quot;color: blue;&quot;&gt;while&lt;/span&gt;(&lt;/code&gt;&lt;span style=&quot;color: blue;&quot;&gt;not&lt;/span&gt; queue.empty()) {&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;         std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;(&quot;&lt;/span&gt; &amp;lt;&amp;lt; queue.front()&amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;, &quot;&lt;/span&gt; &amp;lt;&amp;lt; queue.back() &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;)&quot;&lt;/span&gt;;
         std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;with size:&quot;&lt;/span&gt;&amp;lt;&amp;lt; queue.size() &amp;lt;&amp;lt; std::endl;
         queue.pop();
     }
     std::cout &amp;lt;&amp;lt; std::endl;
}
&lt;span style=&quot;color: blue;&quot;&gt;typedef&lt;/span&gt; Queue&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; &lt;span style=&quot;color: blue;&quot;&gt;intQueue&lt;/span&gt;;&lt;span style=&quot;color: green;&quot;&gt;// It&#39;s common to define typedef&#39;s in order to simply &lt;/span&gt;
&lt;span style=&quot;color: green;&quot;&gt;//definicions&lt;/span&gt;
&lt;span style=&quot;color: green;&quot;&gt;//typedef Queue&amp;lt; intQueue &amp;gt; intQueueQueue&lt;/span&gt;&lt;/code&gt;&lt;span style=&quot;color: green;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;typedef&lt;/span&gt; Queue&amp;lt; Queue&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;gt; intQueueQ&lt;/code&gt;ueue;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;                       &lt;span style=&quot;color: green;&quot;&gt;// ^-- This space isn&#39;t opcional, copiler&#39;d think that is a &amp;lt;&amp;lt; operador&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main() {
     &lt;span style=&quot;color: blue;&quot;&gt;try&lt;/span&gt;{
         &lt;span style=&quot;color: blue;&quot;&gt;intQueue&lt;/span&gt; y;
         y.push(1);
         y.push(2);
         y.push(3);
         y.push(4);
         iterate_Queue(y);

         y.push(15);
         iterate_Queue(y);

         y.front();&lt;span style=&quot;color: green;&quot;&gt;//exception, queue is empty&lt;/span&gt;
     } &lt;span style=&quot;color: blue;&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; std::exception&amp;amp; ex) {
         std::cerr &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;Exception: &quot;&lt;/span&gt; &amp;lt;&amp;lt; ex.what() &amp;lt;&amp;lt; std::endl;
     } 
     
     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;
}&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;b&gt;output:&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: #666666;&quot;&gt;(1, 4)with size:4
(2, 4)with size:3
(3, 4)with size:2
(4, 4)with size:1

(15, 15)with size:1

Exception: Queue&amp;lt;&amp;gt;::front(): empty stack

RUN SUCCESSFUL (total time: 244ms)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;
Ok, I hope you understood the most code, let&#39;s take a look of hard parts.&lt;br /&gt;
&lt;br /&gt;
First, I did a template function to empty and show the values of a Queue, printing the first and the last element.&lt;br /&gt;
&lt;br /&gt;
Beside, we have some exceptions (throws) in our class, pop, back and front. Theses exceptions void the problem to access an empty vector, in that case, we make a throw:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; std::out_of_range(&lt;span style=&quot;color: red;&quot;&gt;&quot;Queue&amp;lt;&amp;gt;::pop(): empty stack&quot;&lt;/span&gt;);&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;Then, in the main, we bracketed our code with a try and catch. This means that C++ is going ahead taking care whether an error happends and if so then execute the catch. In fact, t&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;hat error is called by the throw we made.&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;In the catch, ex.what() get the string we threw.&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;Another example of throw:&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; std::overflow_error(&lt;span style=&quot;color: red;&quot;&gt;&quot;overflow&quot;&lt;/span&gt;);&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; white-space: normal;&quot;&gt;even:&lt;/span&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;throw&lt;/span&gt; 20;&lt;span style=&quot;color: green;&quot;&gt;//in that case you have to catch an int&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;;&quot;&gt;&lt;span style=&quot;white-space: normal;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;;&quot;&gt;&lt;span style=&quot;white-space: normal;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;;&quot;&gt;&lt;span style=&quot;white-space: normal;&quot;&gt;On the other hand, I introduced typedef operator in which you can define any type as you like:&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: green;&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: green;&quot;&gt;//typedef Queue&amp;lt; intQueue &amp;gt; intQueueQueue&lt;/span&gt;&lt;/code&gt;&lt;span style=&quot;color: green;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;typedef&lt;/span&gt; Queue&amp;lt; Queue&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;gt; intQueueQueue;&lt;/code&gt;&lt;/pre&gt;
&lt;code&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;typedef&lt;/span&gt; Queue&amp;lt; std::vector&amp;lt;int&lt;/code&gt;&lt;code&gt;&amp;gt; &amp;gt; intVectorQ&lt;/code&gt;ueue;&lt;/pre&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;code&gt;
I hope you enjoyed. For any doubt or comment, feel free to leave your message.&lt;/code&gt;&lt;/pre&gt;
&lt;code&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;code&gt;
&lt;pre&gt;For an example without use of std::vector :
&lt;pre&gt;&lt;a href=&quot;http://surprising-code.blogspot.com/p/example8.html&quot;&gt;http://surprising-code.blogspot.com/p/example8.html&lt;/a&gt;
&lt;/pre&gt;
&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/code&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/3842203246315742217/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/first-template-class-queue.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3842203246315742217'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3842203246315742217'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/first-template-class-queue.html' title='First template class, queue'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-1851884025013464749</id><published>2012-02-18T00:25:00.001+01:00</published><updated>2012-02-18T00:26:19.419+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Overloading"/><title type='text'>Overloading: calls by reference and by value</title><content type='html'>In this chapter you would understand the significance defining functions types.&lt;br /&gt;
&lt;br /&gt;
By the way, it&#39;s a good idea to pass all arguments by references whenever you don&#39;t have to modify any parameter. In fact, pass by references is much efficient.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;cstring&amp;gt;
&lt;span style=&quot;color: green;&quot;&gt;// maximum of two values of any type (call-by-reference)&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; max(T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; a, T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; b) {
     std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;call-by-reference, 2 arguments&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; a &amp;lt; b ? b : a;
}
&lt;span style=&quot;color: green;&quot;&gt;// maximum of two C-strings (call-by-value)&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* max(&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* a, &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* b) {
     std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;call-by-value&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; (std::strcmp(a, b) &amp;lt; 0) ? b : a;
}
&lt;span style=&quot;color: green;&quot;&gt;// maximum of three values of any type (call-by-reference)&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; max(T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; a, T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; b, T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; c) {
     std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: red;&quot;&gt;&quot;call-by-reference, 3 arguments&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; max(max(a, b), c); &lt;span style=&quot;color: green;&quot;&gt;// error, if max(a,b) uses call-by-value&lt;/span&gt;
}

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main() {
     ::max(1, 2, 3);&lt;span style=&quot;color: green;&quot;&gt;// OK&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; std::endl;
     
     &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;* s1 = &lt;span style=&quot;color: red;&quot;&gt;&quot;surpring&quot;&lt;/span&gt;;
     &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;* s2 = &lt;span style=&quot;color: red;&quot;&gt;&quot;-&quot;&lt;/span&gt;;
     &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;* s3 = &lt;span style=&quot;color: red;&quot;&gt;&quot;code&quot;&lt;/span&gt;;
     ::max(s1, s2, s3);&lt;span style=&quot;color: green;&quot;&gt;// ERROR, warning: returning reference to temporary&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; std::endl; 
     
     ::max(*s1, *s2, *s3);&lt;span style=&quot;color: green;&quot;&gt;//works good! (by reference)&lt;/span&gt;
     std::cout &amp;lt;&amp;lt; std::endl; 
}&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&lt;pre&gt;&lt;code&gt;::max(s1, s2, s3);&lt;span style=&quot;color: green;&quot;&gt;// ERROR, warning: returning reference to temporary&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;This line is given an error/warning because call to the non-template function andhis parameters expected a value not a reference.&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;We may fix up it changing the definition to &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;*&lt;span style=&quot;color: blue;&quot;&gt; const&amp;amp;.&amp;nbsp;&lt;/span&gt;(There are others)&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;If we switch the order the compiler might take another function, for example, if the order is the bellow:&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; max(T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; a, T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; b) {

&lt;span style=&quot;color: blue;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; max(T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; a, T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; b, T &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;&amp;amp; c) {&lt;/code&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color: blue;&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* max(&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* a, &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* b) {&lt;/code&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Now, with this call ::max(s1, s2, s3); we won&#39;t have the warning but, look out, because at this form we aren&#39;t using &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; (std::strcmp(a, b) &amp;lt; 0) ? b : a; .&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;For the moment, as a rule of thumb you always have all&lt;/pre&gt;
&lt;pre&gt;overloaded versions of a function declared before the function is called.&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/1851884025013464749/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/overloading-calls-by-reference-and-by.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1851884025013464749'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1851884025013464749'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/overloading-calls-by-reference-and-by.html' title='Overloading: calls by reference and by value'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-8242566157665406174</id><published>2012-02-17T00:19:00.000+01:00</published><updated>2012-03-05T15:19:23.013+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Pointers and Constants"/><title type='text'>Pointers and Constants</title><content type='html'>Hello everybody,&lt;br /&gt;
This is a fast example to give expression to pointer and constants.&lt;br /&gt;
&lt;br /&gt;
I had some questions about&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* &lt;span style=&quot;color: blue;&quot;&gt;const,&lt;/span&gt;&amp;nbsp;so now, I try to explain what exactly means.&lt;br /&gt;
&lt;br /&gt;
If you continue having questions don&#39;t doubt to ask me.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: blue;&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;

&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; main(){    

     &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* s1 = &lt;span style=&quot;color: red;&quot;&gt;&quot;Testing&quot;&lt;/span&gt;;&lt;span style=&quot;color: green;&quot;&gt;//pointer to const variable&lt;/span&gt;
     &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt;* &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; s2 = &lt;span style=&quot;color: red;&quot;&gt;&quot;constant&quot;&lt;/span&gt;;&lt;span style=&quot;color: green;&quot;&gt;//const pointer&lt;/span&gt;
     &lt;span style=&quot;color: blue;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt;* &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; s3 = &lt;span style=&quot;color: red;&quot;&gt;&quot;aloha&quot;&lt;/span&gt;;&lt;span style=&quot;color: green;&quot;&gt;//const pointer and variable&lt;/span&gt;

     s1 = &lt;span style=&quot;color: red;&quot;&gt;&quot;testing 2&quot;&lt;/span&gt;;&lt;span style=&quot;color: green;&quot;&gt;//works perfectly, reassigning the pointer to another const variable&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//*(s1+1) = &#39;A&#39;; error, tring to change Testing to TAsting&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//*s1 is const, so can&#39;t be modify&lt;/span&gt;
     
      &lt;span style=&quot;color: green;&quot;&gt;//s2 = &quot;new string&quot;; error, read-only, you can&#39;t change the pointer because is constant&lt;/span&gt;
     *s2 = &#39;A&#39;;&lt;span style=&quot;color: green;&quot;&gt;//you can modify the value&lt;/span&gt;

     &lt;span style=&quot;color: green;&quot;&gt;//s3 = &quot;fuuuu&quot;; error, constant pointer&lt;/span&gt;
     &lt;span style=&quot;color: green;&quot;&gt;//*s3 = &#39;B&#39;; error, constant value&lt;/span&gt;
     

     &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; 0;

}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Thanks you all!&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/8242566157665406174/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/pointer-versus-constant.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/8242566157665406174'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/8242566157665406174'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/pointer-versus-constant.html' title='Pointers and Constants'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-7712585547062946696</id><published>2012-02-16T01:35:00.001+01:00</published><updated>2012-02-17T22:12:39.255+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Overloading"/><title type='text'>Overloading Function Templates</title><content type='html'>&lt;style&gt;
.keyword{color:rgb(0,0,255);}
.comment{color:rgb(0,128,0);}
.pp{color:rgb(0,0,255);}
.error{color:rgb(255,0,0);}
.note{color:rgb(143, 0, 255);}
&lt;/style&gt;
&lt;br /&gt;
Hello everybody&lt;br /&gt;
&lt;br /&gt;
So far, we should have a bit idea of templates programming. In this post I&#39;ll speak about overloading functions center of attention to templates.&lt;br /&gt;
&lt;br /&gt;
For a change, I follow using max function.&lt;br /&gt;
&lt;br /&gt;
&lt;style&gt;
.keyword{color:rgb(0,0,255);}
.comment{color:rgb(0,128,0);}
.pp{color:rgb(0,0,255);}
&lt;/style&gt;

&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; mySpace{
    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;&amp;amp; max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;&amp;amp; b){
        std::cout &amp;lt;&amp;lt; &quot;int&quot;&amp;lt;&amp;lt; std::endl;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b;
    }
&lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T &amp;gt;
   &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; T max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; b){
    std::cout &amp;lt;&amp;lt; &quot;template&quot;&amp;lt;&amp;lt; std::endl;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b;
    }
&lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T &amp;gt;
   &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; T max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; b, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; c){
           std::cout &amp;lt;&amp;lt; &quot;template, 3: &quot;&amp;lt;&amp;lt; std::endl;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; max(max(a,b),c);
    }
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    &lt;span class=&quot;comment&quot;&gt;// calls the template with three arguments
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//and this one will call the non-template int function (by argument deduction)
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//because the better choise that compiler can make
&lt;/span&gt;    mySpace::max(2, 1, 9);
 
    &lt;span class=&quot;comment&quot;&gt;//both types match perfectly, so calls non-template int (by argument deduction)
&lt;/span&gt;    mySpace::max(3, 5); 
 
    &lt;span class=&quot;comment&quot;&gt;// if the template can generate a better match, will be selected
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;// there is neither a function with char arguments nor template, so
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;// the call will be max&amp;lt;char&amp;gt; (by argument deduction)
&lt;/span&gt;    mySpace::max(&#39;a&#39;, &#39;b&#39;); 
 
    &lt;span class=&quot;comment&quot;&gt;// calls max&amp;lt;double&amp;gt; (by argument deduction)
&lt;/span&gt;    mySpace::max(3.2, 5.4); 
 
    &lt;span class=&quot;comment&quot;&gt;// forcing the call template with no deduction
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;// calls max&amp;lt;double&amp;gt; (no argument deduction)
&lt;/span&gt;    mySpace::max&amp;lt;&lt;span class=&quot;keyword&quot;&gt;double&lt;/span&gt;&amp;gt;(7, 42); 

    &lt;span class=&quot;comment&quot;&gt;// calls the nontemplate for two ints
&lt;/span&gt;    mySpace::max(&#39;a&#39;, 42.7); 

    &lt;span class=&quot;comment&quot;&gt;// calls max&amp;lt;int&amp;gt; (by argument deduction)
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;// with &amp;lt;&amp;gt;, we are calling the template function
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;// rather that a non-template exists
&lt;/span&gt;    mySpace::max&amp;lt;&amp;gt;(7, 42); 
 
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;

}&lt;/pre&gt;
&lt;br /&gt;
Up to now it seems easy, what happends wheter we start using pointers and char types.&lt;br /&gt;
I like to be practical, so let&#39;s see another one:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;cstring&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;string&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; mySpace{ 
    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T &amp;gt; 
    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; b){ 
        std::cout &amp;lt;&amp;lt; &quot;template&quot;&amp;lt;&amp;lt; std::endl;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; 
    }
    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T &amp;gt; 
    &lt;span class=&quot;note&quot;&gt;//I have changed the argument types to constants because I want to pass it by reference, (&amp;amp;)
&lt;/span&gt;    &lt;span class=&quot;note&quot;&gt;//note that const T&amp;amp; is equivalent to T const&amp;amp;
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; T* &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;&amp;amp;  max (T* &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;&amp;amp; a, T* &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;&amp;amp;  b){ 
        std::cout &amp;lt;&amp;lt; &quot;template pointer: &quot;&amp;lt;&amp;lt; std::endl;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; 
    }
    
    &lt;span class=&quot;comment&quot;&gt;//a fuction for C-string, char*
&lt;/span&gt;    &lt;span class=&quot;note&quot;&gt;/*
     * Note: Go back and remember the first template max; I didn&#39;t 
     * show any call like max(&quot;abc&quot;,&quot;acd&quot;), this one is deduced as 
     * a char*, so, with the implementations we did (return a&amp;gt;b?a:b;),
     * this operacion is comparing the pointers and no his values
     * so this was a bug
     * 
     * In order to resolve it, we may call max&amp;lt;std::string&amp;gt;(&quot;abc&quot;,&quot;acd&quot;)
     * or calling constructor like std::string: max(std::string(&quot;abc&quot;),std::st     * ring(&quot;acd&quot;) )
     * 
     * Or with the following function
     */&lt;/span&gt;
    &lt;span class=&quot;comment&quot;&gt;//
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;* &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;&amp;amp;  max (&lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;* &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;* &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;&amp;amp;  b){ 
        std::cout &amp;lt;&amp;lt; &quot;c-string: &quot;&amp;lt;&amp;lt; std::endl;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; (std::strcmp(a,b) &amp;lt; 0) ? b : a;
    }
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; a = 5, b = 10;
    &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;* p_a = &amp;amp;a;
    &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;* p_b = &amp;amp;b; &lt;span class=&quot;comment&quot;&gt;//pointers_to
&lt;/span&gt;    
    mySpace::max(a,b); &lt;span class=&quot;comment&quot;&gt;//template
&lt;/span&gt;    mySpace::max(p_a,p_b); &lt;span class=&quot;comment&quot;&gt;//template pointer
&lt;/span&gt;    mySpace::max(*p_a,*p_b); &lt;span class=&quot;comment&quot;&gt;//template
&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;* s2 = &quot;I&#39;m so cool&quot;;
    &lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt;* s1 = &quot;David&quot;;
    mySpace::max(s1,s2); &lt;span class=&quot;comment&quot;&gt;//c-string
&lt;/span&gt;    mySpace::max&amp;lt;&amp;gt;(s1,s2); &lt;span class=&quot;comment&quot;&gt;//template, the output is David, WRONG!
&lt;/span&gt;    mySpace::max(&quot;asd&quot;,&quot;asdasd&quot;);&lt;span class=&quot;comment&quot;&gt;//c-string
&lt;/span&gt;    
    std::string s3 = &quot;David&quot;;
    std::string s4 = &quot;I&#39;m so cool&quot;;
    mySpace::max(s3,s4);&lt;span class=&quot;comment&quot;&gt;//c-string
&lt;/span&gt;    
    &lt;span class=&quot;comment&quot;&gt;//This is more typical in C
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt;* s5 = &quot;David&quot;;&lt;span class=&quot;comment&quot;&gt;//&lt;span style=&quot;color: red;&quot;&gt;warning: deprecated conversion from string constant to ‘char*’&lt;/span&gt;
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt;* s6 = &quot;I&#39;m so cool&quot;;&lt;span class=&quot;comment&quot;&gt;//&lt;span style=&quot;color: red;&quot;&gt;warning: deprecated conversion from string constant to ‘char*’&lt;/span&gt;
&lt;/span&gt;    
    &lt;span class=&quot;comment&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;//warning: deprecated conversion from string constant to ‘char*’&lt;/span&gt;
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//our function works with char const*, so call templates,
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//and how templates are constants this make a warning
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//but note that call can be wrong
&lt;/span&gt;    mySpace::max(s5,s6);&lt;span class=&quot;comment&quot;&gt;//template
&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}

    &lt;/pre&gt;
Note that I declared all templates as const and by reference, is a good practice to do unless you need to modify.&lt;br /&gt;
That&#39;s all today, next I will do a bit example with char const* using 3 parameters, like first exercice.</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/7712585547062946696/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/overloading-function-templates.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7712585547062946696'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/7712585547062946696'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/overloading-function-templates.html' title='Overloading Function Templates'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-3593663571752631523</id><published>2012-02-13T16:26:00.000+01:00</published><updated>2012-02-15T23:35:13.326+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Instantiation"/><title type='text'>Argument Deduction</title><content type='html'>&lt;div style=&quot;text-align:justify&quot;&gt;
&lt;style&gt;
.keyword{color:rgb(0,0,255);}
.comment{color:rgb(0,128,0);}
.error{color:rgb(255,0,0);}
.pp{color:rgb(0,0,255);}
&lt;/style&gt;
Hello everybody&lt;br /&gt;
&lt;br /&gt;
Till now, we have been seeing the powerful of templates but, like I showed,&lt;br /&gt;
sometimes we can get some unexpected results so I like to check out this topic, argument deduction&lt;br /&gt;
&lt;br /&gt;
There are so many times that C++ compiler must conclude that T must&lt;br /&gt;
be int or any type, what becomes this?&lt;br /&gt;
&lt;br /&gt;
An example we unexpected:&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;complex&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; mySpace{ &lt;span class=&quot;comment&quot;&gt;// getting sure that this is the class we call becouse std::max is implemented
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T &amp;gt; &lt;span class=&quot;comment&quot;&gt;// for any class/typename T
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; b){ &lt;span class=&quot;comment&quot;&gt;//
&lt;/span&gt;        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; &lt;span class=&quot;comment&quot;&gt;// where the operator &#39;&amp;gt;&#39; is implemented
&lt;/span&gt;    }
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    std::cout &amp;lt;&amp;lt; mySpace::max(10,20) &amp;lt;&amp;lt; std::endl;&lt;span class=&quot;comment&quot;&gt;// works perfectly as we saw
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//std::cout &amp;lt;&amp;lt; mySpace::max(15,21.1) &amp;lt;&amp;lt; std::endl;// &lt;span class=&quot;error&quot;&gt;error: no matching function for call to ‘max(int, double)’&lt;/span&gt;
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//std::cout &amp;lt;&amp;lt; mySpace::max(&#39;a&#39;,21) &amp;lt;&amp;lt; std::endl;// &lt;span class=&quot;error&quot;&gt;error: no matching function for call to ‘max(char, int)’&lt;/span&gt;
&lt;/span&gt;    
    &lt;span class=&quot;comment&quot;&gt;//some ways to fix it:
&lt;/span&gt;    
    &lt;span class=&quot;comment&quot;&gt;// static_cast is a powerful tool, and I&#39;m going to explain it while course
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max(&lt;span class=&quot;keyword&quot;&gt;static_cast&lt;/span&gt;&amp;lt;&lt;span class=&quot;keyword&quot;&gt;double&lt;/span&gt;&amp;gt;(15),21.1) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;comment&quot;&gt;// double(x) call the costructor of double, so 15 is an double
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max(&lt;span class=&quot;keyword&quot;&gt;double&lt;/span&gt;(15),21.1) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;comment&quot;&gt;// this is a bad practice, this cast is highlighting to the compiler that is a double
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//in this case works, but take care it
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max((&lt;span class=&quot;keyword&quot;&gt;double&lt;/span&gt;)15,21.1) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;comment&quot;&gt;//and the first solucion where we really use templates
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//we are forcing that max&#39;s call have to be doouble
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max&amp;lt;&lt;span class=&quot;keyword&quot;&gt;double&lt;/span&gt;&amp;gt;(15,21.1) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;comment&quot;&gt;//this examples also works but returning types is unexpected
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//we unlike 97&#39;s answer, we wanted a, as char
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//but our template function is unable to do it
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max&amp;lt;&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;&amp;gt; (&#39;a&#39;,21) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;comment&quot;&gt;//This works too but don&#39;t make much sense
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;(&#39;a&#39;),21) &amp;lt;&amp;lt; std::endl;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;/pre&gt;
This may appear to be a good method to enable passing two call parameters. Thus, we&#39;ll 
have two templates types as arguments.
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;complex&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; mySpace{ &lt;span class=&quot;comment&quot;&gt;// getting sure that this is the class we call becouse std::max is implemented
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T1 , &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T2 &amp;gt; &lt;span class=&quot;comment&quot;&gt;// for any class/typename T
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T1&amp;amp; max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T1&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T2&amp;amp; b){ &lt;span class=&quot;comment&quot;&gt;//
&lt;/span&gt;        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; &lt;span class=&quot;comment&quot;&gt;// where the operator &#39;&amp;gt;&#39; is implemented
&lt;/span&gt;    }
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    std::cout &amp;lt;&amp;lt; mySpace::max(10,20) &amp;lt;&amp;lt; std::endl;&lt;span class=&quot;comment&quot;&gt;//no problems
&lt;/span&gt;    
    &lt;span class=&quot;comment&quot;&gt;//Now, the errors we had, have been fixed but isn&#39;t enough
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max(15,21.1) &amp;lt;&amp;lt; std::endl;
    std::cout &amp;lt;&amp;lt; mySpace::max(&#39;a&#39;,21) &amp;lt;&amp;lt; std::endl;
    &lt;span class=&quot;comment&quot;&gt;//both of us calls makes this warning
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//&lt;span class=&quot;error&quot;&gt;main.cpp:14:37:   instantiated from here&lt;/span&gt;
&lt;/span&gt;    &lt;span class=&quot;comment&quot;&gt;//&lt;span class=&quot;error&quot;&gt;main.cpp:6:22: warning: returning reference to temporary&lt;/span&gt;
&lt;/span&gt;    
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
This template method have some issues, for example, we&#39;re defining the return type with 
the first argument, then:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;std::cout &amp;lt;&amp;lt; mySpace::max(15,21.1) &amp;lt;&amp;lt; std::endl;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
This call returns 21 (with warning), the comparison&amp;nbsp;works perfectly but at the moment to 
return value, it&#39;s needed an int, so have to truncate the double. In fact, it&#39;s not as simple as truncate
and the code instance a local variable (temporaly), making that our return definacion as constant
is not exaclty correct, so makes a warning.
&lt;br /&gt;
&lt;br /&gt;
Therefore, it may sense to define a returning type, a new template, example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;complex&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; mySpace{ &lt;span class=&quot;comment&quot;&gt;// getting sure that this is the class we call becouse std::max is implemented
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T1, &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T2, &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; RT &amp;gt; &lt;span class=&quot;comment&quot;&gt;// for any class/typename T
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; RT max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T1&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T2&amp;amp; b){ &lt;span class=&quot;comment&quot;&gt;//Note that return type isn&#39;t constant
&lt;/span&gt;        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; &lt;span class=&quot;comment&quot;&gt;// where the operator &#39;&amp;gt;&#39; is implemented
&lt;/span&gt;    }
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    &lt;span class=&quot;comment&quot;&gt;//correct but tedious
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; mySpace::max&amp;lt;&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;,&lt;span class=&quot;keyword&quot;&gt;double&lt;/span&gt;,&lt;span class=&quot;keyword&quot;&gt;double&lt;/span&gt;&amp;gt;(15,21.1) &amp;lt;&amp;lt; std::endl; 
    std::cout &amp;lt;&amp;lt; mySpace::max&amp;lt;&lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt;,&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;,&lt;span class=&quot;keyword&quot;&gt;char&lt;/span&gt;&amp;gt;(&#39;a&#39;,100) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
&lt;br /&gt;
In this way, we can control types and how RT can&#39;t be deduced we have to list explitly each template type..&lt;br /&gt;
Another approach is to specify only the return type, in general we must specify all the argument types up to the last argument that cannot be determined by deduction progress.&lt;br /&gt;
&lt;br /&gt;
Thus, if we change the order of the templates we could only specify RT, let&#39;s see:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-family:courier;font-size:10pt&quot;&gt;&lt;span class=pp&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=pp&gt;#include&lt;/span&gt; &amp;lt;complex&amp;gt;
&lt;span class=keyword&gt;namespace&lt;/span&gt; mySpace{ &lt;span class=comment&gt;// getting sure that this is the class we call becouse std::max is implemented
&lt;/span&gt;    &lt;span class=keyword&gt;template&lt;/span&gt; &amp;lt; &lt;span class=keyword&gt;class&lt;/span&gt; RT, &lt;span class=keyword&gt;class&lt;/span&gt; T1, &lt;span class=keyword&gt;class&lt;/span&gt; T2 &amp;gt; &lt;span class=comment&gt;// for any class/typename T
&lt;/span&gt;    &lt;span class=keyword&gt;inline&lt;/span&gt; RT max (&lt;span class=keyword&gt;const&lt;/span&gt; T1&amp;amp; a, &lt;span class=keyword&gt;const&lt;/span&gt; T2&amp;amp; b){ &lt;span class=comment&gt;//Note that return type isn&#39;t constant
&lt;/span&gt;        &lt;span class=keyword&gt;return&lt;/span&gt; a&amp;gt;b?a:b; &lt;span class=comment&gt;// where the operator &#39;&amp;gt;&#39; is implemented
&lt;/span&gt;    }
}
&lt;span class=keyword&gt;int&lt;/span&gt; main(){
    std::cout &amp;lt;&amp;lt; mySpace::max&amp;lt;&lt;span class=keyword&gt;double&lt;/span&gt;&amp;gt;(15,21.1) &amp;lt;&amp;lt; std::endl; 
    std::cout &amp;lt;&amp;lt; mySpace::max&amp;lt;&lt;span class=keyword&gt;char&lt;/span&gt;&amp;gt;(&#39;a&#39;,100) &amp;lt;&amp;lt; std::endl;
    std::cout &amp;lt;&amp;lt; mySpace::max&amp;lt;&lt;span class=keyword&gt;char&lt;/span&gt;,&lt;span class=keyword&gt;char&lt;/span&gt;,&lt;span class=keyword&gt;int&lt;/span&gt;&amp;gt;(&#39;a&#39;,100) &amp;lt;&amp;lt; std::endl;
    &lt;span class=keyword&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/3593663571752631523/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/argument-deduction.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3593663571752631523'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/3593663571752631523'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/argument-deduction.html' title='Argument Deduction'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-116224161833304768</id><published>2012-02-13T01:17:00.000+01:00</published><updated>2012-02-13T14:06:26.093+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Instantiation"/><title type='text'>Instantiation</title><content type='html'>&lt;br /&gt;
Unfortunately, the tems instance and instantiate are used in a different context in object-oriented programming.&lt;br /&gt;
In general, when I speak about instantisate I will be refering at template context.&lt;br /&gt;
&lt;br /&gt;
When we define a template, compiler review if the systax is correct but this isn&#39;t enough, for example, the example of max. We instantiated some calls to max with int, string, float. All examples worked well because each type had the operator &amp;gt; implemented, the review of this is what I call instantisate.&lt;br /&gt;
&lt;br /&gt;
So, the instantiation&amp;nbsp;is when compiler checks whether some calls like max(a,b) can make it. In the example you&#39;ll see below you can see when compiler make an error for instantisation because our template method is assuming we can use the operator &amp;gt; and isn&#39;t implemented.&lt;br /&gt;
&lt;br /&gt;
Below an example where instantisate makes an error:&lt;br /&gt;
&lt;br /&gt;
&lt;style&gt;
.keyword{color:rgb(0,0,255);}
.comment{color:rgb(0,128,0);}
.error{color:rgb(255,0,0);}
.pp{color:rgb(0,0,255);}
&lt;/style&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;complex&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; mySpace{ &lt;span class=&quot;comment&quot;&gt;// getting sure that this is the class we call becouse std::max is implemented
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T &amp;gt; &lt;span class=&quot;comment&quot;&gt;// for any class/typename T
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; b){ &lt;span class=&quot;comment&quot;&gt;//
&lt;/span&gt;        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; &lt;span class=&quot;comment&quot;&gt;// where the operator &#39;&amp;gt;&#39; is implemented
&lt;/span&gt;    }
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    std::complex&amp;lt;&lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt;&amp;gt; a(10,10.1);
    std::complex&amp;lt;&lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt;&amp;gt; b(10.1,10);
    
    std::cout &amp;lt;&amp;lt; mySpace::max(a,b) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}

&lt;span class=&quot;error&quot;&gt;main.cpp: In function ‘const T&amp;amp; mySpace::max(const T&amp;amp;, const T&amp;amp;) [with T = std::complex&lt;float&gt;]’:&lt;/float&gt;&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;main.cpp:13:34:   instantiated from here
main.cpp:6:22: error: no match for ‘operator&amp;gt;’ in ‘a &amp;gt; b’&lt;/span&gt;
&lt;/pre&gt;
Fixing this error:

&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;complex&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; mySpace{ &lt;span class=&quot;comment&quot;&gt;// getting sure that this is the class we call becouse std::max is implemented
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt; &amp;lt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T &amp;gt; &lt;span class=&quot;comment&quot;&gt;// for any class/typename T
&lt;/span&gt;    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; max (&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; T&amp;amp; b){ &lt;span class=&quot;comment&quot;&gt;//
&lt;/span&gt;        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; &lt;span class=&quot;comment&quot;&gt;// where the operator &#39;&amp;gt;&#39; is implemented
&lt;/span&gt;    }
    &lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; T&amp;gt; 
    &lt;span class=&quot;keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;operator&lt;/span&gt;&amp;gt;(&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; std::complex&amp;lt;T&amp;gt;&amp;amp; a, &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; std::complex&amp;lt;T&amp;gt;&amp;amp; b)    {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(a.imag() &amp;gt; b.imag())
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;
    }
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    std::complex&amp;lt;&lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt;&amp;gt; a(10,10.1);
    std::complex&amp;lt;&lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt;&amp;gt; b(10.1,10);
    
    std::cout &amp;lt;&amp;lt; mySpace::max(a,b) &amp;lt;&amp;lt; std::endl;
    
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;
This solucions is prioritizing &#39;i&#39; (complex number), but we could do anything.

&lt;br /&gt;
&lt;br /&gt;
Next topic, Argument Deduction.&lt;br /&gt;
&lt;br /&gt;
Thanks you all.</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/116224161833304768/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/instantiation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/116224161833304768'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/116224161833304768'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/instantiation.html' title='Instantiation'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-172308377905061528</id><published>2012-02-12T20:34:00.001+01:00</published><updated>2012-02-13T01:18:02.776+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Introduction"/><title type='text'>Why templates?</title><content type='html'>What is a template?&lt;br /&gt;
&lt;br /&gt;
Template programming is a programming method where we don&#39;t indicate what type we are using so this allow to make generals methods or whatever.&lt;br /&gt;
&lt;br /&gt;
For instance, a typical scenario where we can use templates are data structures, such a list or a binary list. We just have indicate what class we are using.&lt;br /&gt;
&lt;span style=&quot;color: #333333; font-family: arial, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;style&gt;
.keyword{color:rgb(0,0,255);}
.comment{color:rgb(0,128,0);}
.pp{color:rgb(0,0,255);}
&lt;/style&gt;&lt;br /&gt;
&lt;b&gt;Undestanding its usefulness:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; MyClass{};&lt;span class=&quot;comment&quot;&gt;//empty
&lt;/span&gt;    
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    std::vector&amp;lt;MyClass&amp;gt; myVector;
        myVector.push_back(MyClass());
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;&lt;br /&gt;
In this example we are adding an empty class to a vector (it&#39;s a bit stupid, I know), note that this class don&#39;t have constructor, C++ makes default.&lt;br /&gt;
&lt;br /&gt;
We can see that vector&amp;nbsp;class&amp;nbsp;is implemented with templates so just putting our class we can use it. Think about it.&lt;br /&gt;
&lt;br /&gt;
Other examples are override methods. C++ allow override methods, for example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;vector&amp;gt;
&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; max (&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; b){
    std::cout &amp;lt;&amp;lt; &quot;max_int&quot; &amp;lt;&amp;lt; std::endl;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; 
}
&lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt; max (&lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt; a, &lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt; b){
    std::cout &amp;lt;&amp;lt; &quot;max_float&quot; &amp;lt;&amp;lt; std::endl;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b?a:b; 
}
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; a = 10, b = 20;
    &lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt; c = 10.1, d = 10.2;
    
    std::cout &amp;lt;&amp;lt; max(a,b) &amp;lt;&amp;lt; std::endl;
    std::cout &amp;lt;&amp;lt; max(c,d) &amp;lt;&amp;lt; std::endl;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;&lt;br /&gt;
Running this returns:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
max_int&lt;br /&gt;
20&lt;br /&gt;
max_float&lt;br /&gt;
10.2&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;So we can see which function have been called.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;The next code show a bit how C++ works and some mistakes we can make if we don&#39;t know it:&lt;/div&gt;&lt;br /&gt;
&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;pp&quot;&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; isMaxA (&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; b){&lt;span class=&quot;comment&quot;&gt;// if a is maxim return true(1) else return false(0)
&lt;/span&gt;    std::cout &amp;lt;&amp;lt; &quot;max_int&quot; &amp;lt;&amp;lt; std::endl;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a&amp;gt;b; 
}

&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; main(){
    &lt;span class=&quot;keyword&quot;&gt;float&lt;/span&gt; c = 10.2, d = 10.1;
    
    std::cout &amp;lt;&amp;lt; isMaxA(c,d) &amp;lt;&amp;lt; std::endl;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; 0;
}
&lt;span class=&quot;comment&quot;&gt;/*
This program print this:
max_int
0&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;font-family: courier; font-size: 10pt;&quot;&gt;&lt;span class=&quot;comment&quot;&gt;*/&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
This isn&#39;t the answer we expected. And why is this happening?&lt;br /&gt;
C++ try to find a function to call isMaxA where it takes float arguments but cannot so &amp;nbsp;call isMaxA with int arguments. Transforming float to int ( by trucation ) so out function compares 10&amp;gt;10 and, indeed, the answer is no instead of 10.2&amp;gt;10.1&lt;br /&gt;
&lt;br /&gt;
So with this fool example we can see weaknesses and, in fact, it&#39;s a bit stupid to declare two methods that are exacly the same.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solucion (templates):&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;font-family:courier;font-size:10pt&quot;&gt;&lt;span class=pp&gt;#include&lt;/span&gt; &amp;lt;iostream&amp;gt;
&lt;span class=pp&gt;#include&lt;/span&gt; &amp;lt;cstring&amp;gt;
&lt;span class=keyword&gt;namespace&lt;/span&gt; mySpace{ &lt;span class=comment&gt;// getting sure that this is the class we call becouse std::max is implemented
&lt;/span&gt;    &lt;span class=keyword&gt;template&lt;/span&gt; &amp;lt; &lt;span class=keyword&gt;class&lt;/span&gt; T &amp;gt; &lt;span class=comment&gt;// for any class/typename T
&lt;/span&gt;    &lt;span class=keyword&gt;inline&lt;/span&gt; &lt;span class=keyword&gt;const&lt;/span&gt; T&amp;amp; max (&lt;span class=keyword&gt;const&lt;/span&gt; T&amp;amp; a, &lt;span class=keyword&gt;const&lt;/span&gt; T&amp;amp; b){ &lt;span class=comment&gt;//
&lt;/span&gt;        &lt;span class=keyword&gt;return&lt;/span&gt; a&amp;gt;b?a:b; &lt;span class=comment&gt;// where the operator &#39;&amp;gt;&#39; is implemented
&lt;/span&gt;    }
}
&lt;span class=keyword&gt;int&lt;/span&gt; main(){
    &lt;span class=keyword&gt;int&lt;/span&gt; a = 10, b=20;
    &lt;span class=keyword&gt;float&lt;/span&gt; c = 10.2, d = 10.1;
    std::string e = &amp;quot;a&amp;quot;;
    std::string f = &amp;quot;aa&amp;quot;;
    
    std::cout &amp;lt;&amp;lt; mySpace::max(a,b) &amp;lt;&amp;lt; std::endl;
    std::cout &amp;lt;&amp;lt; mySpace::max(c,d) &amp;lt;&amp;lt; std::endl;
    std::cout &amp;lt;&amp;lt; mySpace::max(e,f) &amp;lt;&amp;lt; std::endl;
    std::max(a,b);
    &lt;span class=keyword&gt;return&lt;/span&gt; 0;
}&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
That&#39;s all today, I&#39;ll explain slowly soon why I have used words like inline, namespace, class, etc&lt;br /&gt;
Just get the idea&lt;br /&gt;
See you soon&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/172308377905061528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/why-templates.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/172308377905061528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/172308377905061528'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/why-templates.html' title='Why templates?'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-880778854866581868.post-1033632582311195304</id><published>2012-02-12T18:09:00.000+01:00</published><updated>2012-03-28T20:03:05.117+02:00</updated><title type='text'>Introduction of myself and the goal of this blog</title><content type='html'>Welcome everybody!&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Why are you here and why you won&#39;t leave?&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
If you are a programmer who&#39;d like to improve his skills programming, making better practices or you&lt;br /&gt;
just are interested in new ways to programming or even more powerful tools, you are at right site.&lt;br /&gt;
&lt;br /&gt;
By now, this course/blog is oriented to the use of C++ templates but likely I will do something more like some articles of interest. I&#39;m assuming that if you are here is because you know programming so I won&#39;t explain anything of compiles, IDE&#39;s or whatever.&lt;br /&gt;
&lt;br /&gt;
Beside, I&#39;ll try to teach and explain all I do but if you have any doubt you can ask me. My form&lt;br /&gt;
to teach maybe is a bit particular because I think that read posts isn&#39;t enough whenever you have&lt;br /&gt;
to program, how a writer learn to write? reading? no, writing. Of course that read would be good&lt;br /&gt;
but again, not enough.&lt;br /&gt;
&lt;br /&gt;
Something I like to teach (this is not an index, it&#39;s a brainstorm ideas):&lt;br /&gt;
&lt;br /&gt;
Why templates?&lt;br /&gt;
Function Templates&lt;br /&gt;
Argument Deduction&lt;br /&gt;
Instantiation&lt;br /&gt;
Polymorphic Power of Templetes&lt;br /&gt;
Namespaces&lt;br /&gt;
and more!&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;About myself&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
I&#39;m a studient of computer engineering at Universitat Pompeu Fabra ( Barcelona ) and I did this blog with the goal of improve my english and show the power of templates in C++.&lt;br /&gt;
&lt;br /&gt;
Thus, if there is any part that is hard to understand feel free to advise me.&lt;br /&gt;
&lt;br /&gt;
If you would like to deepen in any subject or talk about any specific just say it!&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;color: #333333; font-family: arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://surprising-code.blogspot.com/feeds/1033632582311195304/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://surprising-code.blogspot.com/2012/02/introduction-of-myself-and-goal-of-this.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1033632582311195304'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/880778854866581868/posts/default/1033632582311195304'/><link rel='alternate' type='text/html' href='http://surprising-code.blogspot.com/2012/02/introduction-of-myself-and-goal-of-this.html' title='Introduction of myself and the goal of this blog'/><author><name>David</name><uri>http://www.blogger.com/profile/05834327291716242832</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>