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

<channel>
	<title>TechieVarta with C++ Guru</title>
	<atom:link href="http://cppguru.techievarta.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cppguru.techievarta.com</link>
	<description>Just another Techie Varta</description>
	<pubDate>Mon, 04 Jan 2010 02:25:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Boost.Spirit: Semantic Actions</title>
		<link>http://cppguru.techievarta.com/2010/01/01/boostspirit-semantic-actions/</link>
		<comments>http://cppguru.techievarta.com/2010/01/01/boostspirit-semantic-actions/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 23:07:06 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[boost]]></category>

		<category><![CDATA[boost.spirit]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[parser]]></category>

		<guid isPermaLink="false">http://cppguru.techievarta.com/?p=40</guid>
		<description><![CDATA[Note that this post applies to Spirit.Classic or 2.0.
In the first two posts we introduced basic parsing techniques defined by Spirit. The parsing is only useful, if we can do something with parsing results. In Spirit you achieve this with semantic actions.
Semantic actions are expected to use functional programming paradigms. The most basic semantic action [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note that this post applies to Spirit.Classic or 2.0</strong>.</p>
<p>In the first two posts we introduced basic parsing techniques defined by Spirit. The parsing is only useful, if we can do something with parsing results. In Spirit you achieve this with semantic actions.</p>
<p>Semantic actions are expected to use functional programming paradigms. The most basic semantic action has the following prototype:</p>
<pre><code><span><span style="color: #000000"><span class="keyword"> void </span><span class="identifier">f</span><span class="special">(</span><span class="identifier">IteratorT </span><span class="identifier">first</span><span class="special">, </span><span class="identifier">IteratorT </span><span class="identifier">last</span><span class="special">);

</span></span></span></code></pre>
<p><code><span><span style="color: #000000"><span class="special">or as functor</span></span></span></code></p>
<pre><code><span><span style="color: #000000"><span class="special">  </span><span class="keyword">struct </span><span class="identifier">my_functor
    </span><span class="special">{
        </span><span class="keyword">void </span><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">IteratorT </span><span class="identifier">first</span><span class="special">, </span><span class="identifier">IteratorT </span><span class="identifier">last</span><span class="special">) </span><span class="keyword">const</span><span class="special">;
    </span><span class="special">};

</span></span></span></code></pre>
<p>Here is an example of simple action from Spirit user guide:</p>
<pre><code><span><span style="color: #000000"><span class="special"> </span><span class="keyword">void
    </span><span class="identifier">my_action</span><span class="special">(</span><span class="keyword">char const</span><span class="special">* </span><span class="identifier">first</span><span class="special">, </span><span class="keyword">char const</span><span class="special">* </span><span class="identifier">last</span><span class="special">)
    {
        </span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special"> </span><span class="identifier">str</span><span class="special">(</span><span class="identifier">first</span><span class="special">, </span><span class="identifier">last</span><span class="special">);
        </span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout </span><span class="special">&lt;&lt; </span><span class="identifier">str </span><span class="special">&lt;&lt; </span><span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;
    }

</span></span></span></code></pre>
<p>Applying the action is rather simple. You specify them in [] after the rule. From previous e-mail address parser, you can write:</p>
<pre><code><span><span><span><span style="color: #000000"><span class="special"> </span><span class="identifier">
    r </span><span class="special">= *(mailTo | anychar_p);
    mailTo = "mailTo:" &gt;&gt; emailAddress[&amp;my_action];
    emailAddress = </span></span></span></span></span></code><code>lexeme_d[ +alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p)];</code></pre>
<p>my_action will be called with iterator pointing to start and end of the parsed e-mail address. The above action will result in printing all the e-mail addresses in the input.</p>
<p>In a lot of cases, it is wasteful to call actions with the Iterators pointing to first and last. After all, Spirit has just parsed the contents. For this purpose the boost defines specialized actions. For example</p>
<pre><code><span><span style="color: #000000"><span class="identifier">  </span><span class="keyword">void </span><span class="identifier">func</span><span class="special">(</span><span class="identifier">NumT </span><span class="identifier">val</span><span class="special">);
</span></span></span></code></pre>
<p>or equivalent functor</p>
<pre><code><span><span style="color: #000000"><span class="keyword">struct </span><span class="identifier">fctr
    </span><span class="special">{
        </span><span class="keyword">void </span><span class="keyword">operator</span><span class="special">()(</span><span class="identifier">NumT </span><span class="identifier">val</span><span class="special">) </span><span class="keyword">const</span><span class="special">;
    </span><span class="special">};</span></span></span></code></pre>
<p>can be applied to any numeric parsers (real_p, ureal_p, int_p, uint_p). Similar actions exist for other other types of parsers. Please check Spirit guide for details.</p>
<p>The complete program looks like:</p>
<p>#include &lt;boost/spirit/core.hpp&gt;<br />
#include &lt;iostream&gt;<br />
using namespace boost::spirit;</p>
<p>void<br />
my_action(const char* first, const char* last)<br />
{<br />
std::string str(first, last);<br />
std::cout &lt;&lt; str &lt;&lt; std::endl;<br />
}</p>
<p>struct my_grammar : public grammar&lt;my_grammar&gt;<br />
{<br />
template &lt;typename ScannerT&gt;<br />
struct definition<br />
{<br />
rule&lt;ScannerT&gt;  r, mailTo, emailAddress;<br />
definition(my_grammar const&amp; self)  {<br />
r = *(mailTo | anychar_p);<br />
mailTo = &#8220;mailTo:&#8221; &gt;&gt; emailAddress[&amp;my_action];<br />
emailAddress = lexeme_d[ +alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p)];<br />
}<br />
rule&lt;ScannerT&gt; const&amp; start() const { return r; }<br />
};<br />
};</p>
<p>int main(){<br />
const char* str = &#8220;mailTo:a@b.com  test mailTo:d@f.com&#8221;;<br />
my_grammar g;<br />
if (parse(str, str + strlen(str), g, space_p).full)<br />
std::cout &lt;&lt; &#8220;parsing succeeded\n&#8221;;<br />
else<br />
std:: cout &lt;&lt; &#8220;parsing failed\n&#8221;;<br />
return 0;<br />
}<br />
In addition, there is a large selection of pre-defined actions. You an find them<a href="http://www.boost.org/doc/libs/1_36_0/libs/spirit/classic/doc/predefined_actors.html"> here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2010/01/01/boostspirit-semantic-actions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Functional Programming &#38; Boost.Lambda Programming</title>
		<link>http://cppguru.techievarta.com/2009/12/26/functional-programming-boostlambda-programming/</link>
		<comments>http://cppguru.techievarta.com/2009/12/26/functional-programming-boostlambda-programming/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 16:24:54 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[boost]]></category>

		<category><![CDATA[templates]]></category>

		<category><![CDATA[boost.spirit]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<guid isPermaLink="false">http://cppguru.techievarta.com/?p=36</guid>
		<description><![CDATA[I will like to take a break from Boost.Spirit related topic to talk about Functional Programming. Understanding of Functional Programming is essential for understanding how Spirit related actions are implemented. Typical object oriented programming paradigm combines mutable data and set of algorithms which operates on data. In contrast FP avoids mutable data or state and [...]]]></description>
			<content:encoded><![CDATA[<p>I will like to take a break from Boost.Spirit related topic to talk about Functional Programming. Understanding of Functional Programming is essential for understanding how Spirit related actions are implemented. Typical object oriented programming paradigm combines mutable data and set of algorithms which operates on data. In contrast FP avoids mutable data or state and emphasizes on application of functions. From Wikipedia:</p>
<blockquote><p>In practice, the difference between a mathematical function and the notion of a &#8220;function&#8221; used in imperative programming is that imperative functions can have side effects, changing the value of already calculated computations. Because of this they lack referential transparency, i.e. the same language expression can result in different values at different times depending on the state of the executing program. Conversely, in functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function <em>f</em> twice with the same value for an argument <em>x</em> will produce the same result <em>f(x)</em> both times. Eliminating side-effects can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.</p></blockquote>
<p>STL provides set of algorithms which can be viewed as FP. For example std::copy will always copy source to destination.</p>
<p>The next concept in FP is called currying, technique in which a function is applied to its arguments one at a time, with each application returning a new function that accepts the next argument. The STL function <a href="http://www.sgi.com/tech/stl/binder1st.html">bind1st</a>, <a href="http://www.sgi.com/tech/stl/binder2nd.html">bind2nd</a> and <a href="http://www.sgi.com/tech/stl/binary_compose.html">binary_compose</a> can be used for currying</p>
<p>All funcitonal programming languages are based on Lambda Calculus introduced in 1930s which is based on anonymous functions and currying. You can read more on it<a href="http://en.wikipedia.org/wiki/Lambda_calculus"> here</a>.</p>
<p>Boost.Lambda and Boost.Phoenix introduce formal system of funcitonal programming and <a href="http://en.wikipedia.org/wiki/Lambda_calculus">lambda expressions</a> to C++. I typically use Boost.Lambda. I only use Phoenix when using Spirit since they are more closely integrated. They share a lot of similar concept. This post will discuss Boost.Lambda. The next post will use these concepts to introduce Phonix as applied to Boost.Spirit.</p>
<p>Let us start with a simple lambda expression _1 = 1 is definition of an anonymous function which takes one argument and sets it to 1. So to initialize a variable to 1 you could write:</p>
<p>(_1 + 1)(i);</p>
<p>or if you wanted to initialize all variables in a container to 1, you would write something like:</p>
<p>std::vector v;</p>
<p>std::for_each(v.begin(), v.end(), _1=1);</p>
<p>or to print everyobject in a container:</p>
<p>std::for_each(v.begin(), v.end(), std::cout &lt;&lt; _1);</p>
<p>What if you wanted to define a function with calls a function with one argument and assigns results to the same argument:</p>
<p>That function will be _1 = bind(foo, _1);</p>
<p>Bind is generalized version of std::bind1st and bind2nd. With the above expression in place one can write:</p>
<p>(_1 = bind(foo, _1))(i);</p>
<p>Which will be equivalent to:</p>
<p>i = foo(i);</p>
<p>So with for_each one can write:</p>
<p>std::for_each(v.begin(), v.end(); _1 = bind(foo, _1));</p>
<p>_1 in the above examples are called placeholders which are equlvalent of lambda in the lambda expression. The above can be generalized with larger number of placeholders. For example one can write: (_1 + _2) to create a function which adds its two arguments. Boost Lambda defines up to 3 place holders. Higher order functions can be constructed using currying.</p>
<p>A function definition which used _2 takes 2 arguments and one which uses _3 takes 3 arguments.</p>
<p>For example _3 = 1, takes 3 argument and</p>
<p>(_3 = 1)(k) will have compile time errors.</p>
<p>(_3 = 1)(i,j,k) is good and is equivalent to k = 1.</p>
<p>Above, we are using simple expressions to create anonymous functions which do not have side effects and depend only on their arguments. As alluded earlier BLL also provides bind expression as generalization of bind1st and bind2nd. With BLL bind expression it is possible to bind any method with up to 9 arguments for delayed executions. It can target C++ class members, or simple functions as target. The syntax used is similar to bind1st and bind2nd.</p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/12/26/functional-programming-boostlambda-programming/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Boost.Spirit : Complete Parser Design</title>
		<link>http://cppguru.techievarta.com/2009/12/25/boostspirit-complete-parser-design/</link>
		<comments>http://cppguru.techievarta.com/2009/12/25/boostspirit-complete-parser-design/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 18:13:38 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[boost]]></category>

		<category><![CDATA[templates]]></category>

		<category><![CDATA[Add new tag]]></category>

		<category><![CDATA[bnf]]></category>

		<category><![CDATA[boost.spirit]]></category>

		<category><![CDATA[C++ Reference]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://cppguru.techievarta.com/?p=30</guid>
		<description><![CDATA[Note that this post applies to Spirit.Classic or 2.0.
In the last post we discussed how to write a simple parser using Spirit. Most real life parsers are a lot more complex requiring several rules and combining them to create a full grammar. Spirit provides support to declare a full grammar. Let us create a parser [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note that this post applies to Spirit.Classic or 2.0</strong>.</p>
<p>In the <a href="http://cppguru.techievarta.com/2009/12/20/boostspirit-easy-to-use-parser/">last post</a> we discussed how to write a simple parser using Spirit. Most real life parsers are a lot more complex requiring several rules and combining them to create a full grammar. Spirit provides support to declare a full grammar. Let us create a parser which looks for all mailto: tag with associated e-mail address in an input. All Spirit grammar are derived from grammar class. The following is definition of grammar:</p>
<pre><code><span><span style="color: #000000"><span class="special"> </span><span class="keyword">struct </span><span class="identifier">my_grammar </span><span class="special">: </span><span class="keyword">public </span><span class="identifier">grammar</span><span class="special">&lt;</span><span class="identifier">my_grammar</span><span class="special">&gt;
    </span><span class="special">{
        </span><span class="keyword">template </span><span class="special">&lt;</span><span class="keyword">typename </span><span class="identifier">ScannerT</span><span class="special">&gt;
        </span><span class="keyword">struct </span><span class="identifier">definition
        </span><span class="special">{
            </span><span class="identifier">rule</span><span class="special">&lt;</span><span class="identifier">ScannerT</span><span class="special">&gt;  </span><span class="identifier">r</span><span class="special">;
            </span><span class="identifier">definition</span><span class="special">(</span><span class="identifier">my_grammar </span><span class="keyword">const</span><span class="special">&amp; </span><span class="identifier">self</span><span class="special">)  </span><span class="special">{ </span><span class="identifier">r </span><span class="special">= </span><span class="comment">/*..define here..*/</span><span class="special">; </span><span class="special">}
            </span><span class="identifier">rule</span><span class="special">&lt;</span><span class="identifier">ScannerT</span><span class="special">&gt; </span><span class="keyword">const</span><span class="special">&amp; </span><span class="identifier">start</span><span class="special">() </span><span class="keyword">const </span><span class="special">{ </span><span class="keyword">return </span><span class="identifier">r</span><span class="special">; </span><span class="special">}
        </span><span class="special">};
    </span><span class="special">};

</span></span></span></code></pre>
<p>The my_grammar is derived from grammar class using curiously recurring template pattern (CRTP). For those who are new to CRTP, it was introduced by <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern">Jim Copelien and Wikipedia </a>has some details on it. Using CRTP it is possible to achieve effect of dynamic polymorphism without taking cost of virtual function. More on CRTP another day.</p>
<p>Each grammar class must define another nested structure called definition. The definition must have a function called start which returns starting parser rule. So let us start writing rules for e-mail address parser. The mailto rule can be written as:</p>
<pre>     mailTo = "mailto:" &gt;&gt; emailAddress;</pre>
<p>From our previous post, emailAddress can be written as:</p>
<pre>     emailAddress =  <code>lexeme_d[ +alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p)];
</code></pre>
<p>Of course the full input has things other than the mailto: tags. So we must skip other characters. You do that as follows:</p>
<pre>       r = mailTo | anychar_p;</pre>
<p>The above rule is saying that try matching the input with mailTo and if it does not match mailTo rule, match it against any character parser.</p>
<p><code><span><span style="color: #000000"><span class="special">So combining these two the grammar constructor now can be written as:</span></span></span></code></p>
<pre><code><span><span><span><span style="color: #000000"><span class="special">      </span><span class="identifier">definition</span><span class="special">(</span><span class="identifier">my_grammar </span><span class="keyword">const</span><span class="special">&amp; </span><span class="identifier">self</span><span class="special">)  </span><span class="special">{ </span><span class="identifier">
            r </span><span class="special">= mailTo | anychar_p;
            mailTo = "mailTo:" &gt;&gt; emailAddress;
            emailAddress = </span></span></span></span></span></code><code>lexeme_d[ +alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p)];</code>
<code><span><span><span><span style="color: #000000"><span class="special">      </span><span class="special">}
</span></span></span></span></span></code></pre>
<p>First thing one notices is that the rule &#8220;r&#8221; refers to rules &#8220;emailAddress&#8221; and &#8220;mailTo&#8221; before they are initialized. It works because the rules are held by reference and not by value. The referred rule can be initialized anytime. This does complicate programming a little bit. It means that it is user&#8217;s responsibility to make sure that the referred rules never go out of scope. So emailAddress cannot be local to definition. Typically, one declares all rules as part of the definition call definition. The full grammar now becomes:</p>
<pre><code><span><span><span><span style="color: #000000"><span class="special"> </span><span class="keyword">struct </span><span class="identifier">my_grammar </span><span class="special">: </span><span class="keyword">public </span><span class="identifier">grammar</span><span class="special">&lt;</span><span class="identifier">my_grammar</span><span class="special">&gt;
    </span><span class="special">{
        </span><span class="keyword">template </span><span class="special">&lt;</span><span class="keyword">typename </span><span class="identifier">ScannerT</span><span class="special">&gt;
        </span><span class="keyword">struct </span><span class="identifier">definition
        </span><span class="special">{
            </span><span class="identifier">rule</span><span class="special">&lt;</span><span class="identifier">ScannerT</span><span class="special">&gt;  </span><span class="identifier">r</span><span class="special">;
</span></span></span></span></span></code><code><span><span><span><span><span><span><span><span style="color: #000000"><span class="identifier">            rule</span><span class="special">&lt;</span><span class="identifier">ScannerT</span><span class="special">&gt;  </span><span class="identifier">emailAddress mailTo</span><span class="special">;</span></span></span></span></span></span></span></span></span></code>
<code><span><span><span><span style="color: #000000"><span class="special">            </span><span class="identifier">definition</span><span class="special">(</span><span class="identifier">my_grammar </span><span class="keyword">const</span><span class="special">&amp; </span><span class="identifier">self</span><span class="special">)  </span><span class="special">{ </span><span class="identifier">
                r </span><span class="special">= mailTo | anychar_p;
                mailTo = "mailTo:" &gt;&gt; emailAddress;
                emailAddress = </span></span></span></span></span></code><code>lexeme_d[ +alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p)];</code>
<code><span><span><span><span style="color: #000000"><span class="special">      </span><span class="special">      }
</span></span></span></span></span></code><code><span><span><span><span style="color: #000000"><span class="special">            </span><span class="identifier">rule</span><span class="special">&lt;</span><span class="identifier">ScannerT</span><span class="special">&gt; </span><span class="keyword">const</span><span class="special">&amp; </span><span class="identifier">start</span><span class="special">() </span><span class="keyword">const </span><span class="special">{ </span><span class="keyword">return </span><span class="identifier">r</span><span class="special">; </span><span class="special">}
        </span><span class="special">};
    </span><span class="special">};
</span></span></span></span></span></code></pre>
<p>OK! now we have the grammar. Let us use it.</p>
<pre><code><span><span style="color: #000000"><span class="special">   </span><span class="identifier">my_grammar </span><span class="identifier">g</span><span class="special">;
    </span><span class="keyword">if </span><span class="special">(</span><span class="identifier">parse</span><span class="special">(</span><span class="identifier">first</span><span class="special">, </span><span class="identifier">last</span><span class="special">, </span><span class="identifier">g</span><span class="special">, </span><span class="identifier">space_p</span><span class="special">).</span><span class="identifier">full</span><span class="special">)
        </span><span class="identifier">cout </span><span class="special">&lt;&lt; </span><span class="string">"parsing succeeded\n"</span><span class="special">;
    </span><span class="keyword">else
        </span><span class="identifier">cout </span><span class="special">&lt;&lt; </span><span class="string">"parsing failed\n"</span><span class="special">;</span></span></span></code></pre>
<p><code><span><span><span><span style="color: #000000"><span class="special">Here first and last are iterators pointing to first and the last characters in the input. </span></span></span></span></span></code></p>
<p><span><span><span><span style="color: #000000"><span class="special">Note that the above grammar matches exactly one e-mail address or any other character. This can be easily be modified to match all e-mail addresses.</span></span></span></span></span></p>
<p>The new rule will be:</p>
<p>r = *(mailTo | anychar_p);</p>
<p><span><span><span><span style="color: #000000"><span class="special"><br />
</span></span></span></span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/12/25/boostspirit-complete-parser-design/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Boost.Spirit - Easy to use Parser</title>
		<link>http://cppguru.techievarta.com/2009/12/20/boostspirit-easy-to-use-parser/</link>
		<comments>http://cppguru.techievarta.com/2009/12/20/boostspirit-easy-to-use-parser/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 04:13:29 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[boost]]></category>

		<category><![CDATA[bnf]]></category>

		<category><![CDATA[boost.spirit]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[parser]]></category>

		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://cppguru.techievarta.com/?p=23</guid>
		<description><![CDATA[Note that this post applies to Spirit.Classic or 2.0.
I recently had write a parser in C++ and decided to give Boost.Spirit a chance. I was delighted with ease of use of the parser itself. It was a steep learning curve to get started. However, once I got started, it made life significantly simple.
Boost.Spirit provides a [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note that this post applies to Spirit.Classic or 2.0</strong>.</p>
<p>I recently had write a parser in C++ and decided to give<a href="http://boost-spirit.com/home/"> Boost.Spirit</a> a chance. I was delighted with ease of use of the parser itself. It was a steep learning curve to get started. However, once I got started, it made life significantly simple.</p>
<p>Boost.Spirit provides a very simple way to create a parser using EBNF grammar. Let us consider an e-mail address parser as an example.<br />
<code>+alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p);</code><br />
Simple! Let us understand the above statement. In Boost.Spirit rule&lt;&gt; template defines basic grammar rule. A rule is made up of parsers and other rules. In Boost.Spirit built-in parser all have _p as suffix. The alnum_p parser matches any character or number. &#8216;+&#8217; represents operator which matches one more more instances of the enclosed parser.</p>
<p>&#8216;&gt;&gt;&#8217; is an overloaded operator which simply says followed by.&#8217;@&#8217; is short form of parser which matches character &#8216;@&#8217;.</p>
<p>So the above statement says any alphanumeric string followed by &#8216;@&#8217; followed by any other alpha numeric string. This can than be followed by any number of &#8220;.&lt;alphanumber&gt;&#8221;.</p>
<p>By default the &gt;&gt; includes white space skipping. This implies the above parser will match (a@b as well as a @ b).</p>
<p>We don&#8217;t want white space skipping for e-mail address. So we need to modify the above rule such that &gt;&gt; does not skip white space. For this and other purposes, the Spirit allows directives. These are special instructions which can be applied to a part of the rule. No white space skipping is achieved by using lexeme_d.<br />
<code>lexeme_d[ +alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p);];</code></p>
<p>I have no clue why the name. But the suffix is _d for all directives.OK! we got a rule. Now let us check if this rule matches a specified string:</p>
<p><code>bool isEmailAddress(const std::string&amp; str){<br />
return parse(str, lexeme_d[ +alnum_p &gt;&gt; '@' &gt;&gt; +alnum_p &gt;&gt; *('.' &gt;&gt; +alnum_p);], space_p).full;<br />
}<br />
</code><br />
The above statement will check if the supplied string is an e-mail address or not.</p>
<p>In the next post we will discuss how to create more complex examples as well as deal with parse actions.</p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/12/20/boostspirit-easy-to-use-parser/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C++ Concept Part 3</title>
		<link>http://cppguru.techievarta.com/2009/01/25/c-concept-part-3/</link>
		<comments>http://cppguru.techievarta.com/2009/01/25/c-concept-part-3/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 05:03:33 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[c++0x]]></category>

		<category><![CDATA[templates]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ Reference]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://cppguru.isa-geek.com/?p=101</guid>
		<description><![CDATA[This is the last post in series of posts on C++0x concepts. In this we will look at C++0x concepts. The C++0x standard for concepts is almost identical to ConceptC++. The syntax are almost identical. Significant parts of ConceptC++ have become part of the draft standard.
One of the things we have not looked at the [...]]]></description>
			<content:encoded><![CDATA[<p>This is the last post in series of posts on C++0x concepts. In this we will look at C++0x concepts. The C++0x standard for concepts is almost identical to ConceptC++. The syntax are almost identical. Significant parts of ConceptC++ have become part of the draft standard.</p>
<p>One of the things we have not looked at the definition of concept map. In the previous example of sort, the template takes ForwardIterator as an argument. The article did not discuss what makes a forward iterator. Can we use an int* as a forard iterator? We have also said that ForwardIterator must has value_type argument. int* does not have a member value_type. But we want to pass int* as parameter to sort so that for example, we can sort an array of integers.</p>
<p>C++0x provides a way to solve this problem called concept_map. Using concept_map we can define what constitute ForwardIterator.</p>
<pre><code>template ;
	concept_map ForwardIterator {
                                    // T*'s value_type is T
		typedef T value_type;
	};
</code></pre>
<p>Using above syntax we are saying that any T&#8221;*&#8217;s value_type is T. This removes need to wrap int* in another container.</p>
<p>In all C++0x concept provides solution for one of the biggest ongoing frustration with generic programming with C++. It will provide early and clear compilation errors for template users and authors both and bind them in template parameter contract.</p>
<h2>Other C++0x sources on the web</h2>
<ul>
<li><a href="http://www.research.att.com/~bs/C++0xFAQ.html#concepts">Bjarne Stroustru&#8217;s C++0x FAQ</a></li>
<li><a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Concepts">Wikipedia</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/01/25/c-concept-part-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C++0x Concepts Contd..</title>
		<link>http://cppguru.techievarta.com/2009/01/18/c0x-concepts-contd/</link>
		<comments>http://cppguru.techievarta.com/2009/01/18/c0x-concepts-contd/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 02:34:17 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[c++0x]]></category>

		<category><![CDATA[templates]]></category>

		<category><![CDATA[boost]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ Reference]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://cppguru.isa-geek.com/?p=93</guid>
		<description><![CDATA[In previous article we had discussed BCCL. This post continues to discuss Concept with ConceptC++. The ConceptC++ was the playground for language level Concept ideas which eventually became part of currently proposed C++0x standard. In this post we take in-depth view to ConceptC++. These extensions were implemented by modifying GCC.
ConceptC++
Unlike the Boost Concept Check Library [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://cppguru.isa-geek.com/2009/01/c0x-concepts/">previous article</a> we had discussed BCCL. This post continues to discuss Concept with ConceptC++. The ConceptC++ was the playground for language level Concept ideas which eventually became part of currently proposed C++0x standard. In this post we take in-depth view to <a href="http://www.generic-programming.org/languages/conceptcpp/">ConceptC++</a>. These extensions were implemented by modifying GCC.</p>
<h2>ConceptC++</h2>
<p>Unlike the Boost Concept Check Library (BCCL), which is implemented purely using standard C++ constructs, these extensions are implemented by extending C++ language. By changing the C++ definition, it is possible to do things which was not possible otherwise. In particular, remember that with BCCL, it was not possible to make template parameter restriction part of template or function definition. With ConceptC++, this becomes now possible. In addition to enforcing restriction on instantiation of template, ConceptC++ also imposes restrictions on template implementation and makes sure that it only uses template parameter&#8217;s facility defined in the contract.</p>
<p>To understand ConceptC++, let us consider sum template which adds two parameters and returns the value:</p>
<pre>template &lt;typename T&gt;</pre>
<pre>T sum(T a, T b){</pre>
<pre>   return a+b;</pre>
<pre>}</pre>
<p>With Concept C++ we can rewrite this to:</p>
<pre>template &lt;CopyConstructable T&gt;</pre>
<pre>T sum(T a, T b){</pre>
<pre>   return a+b;</pre>
<pre>}</pre>
<p>This says T must be copy constructable. If we compile this template using ConceptC++ (even without any instantiation) we get the following error:</p>
<pre>error: no match for 'operator+' in 'a+b'</pre>
<p>That is because the template definition did not require T to implement operator+ but is trying to use it. This forces template signature to accurately define the template signature as:</p>
<pre>template &lt;CopyConstructable T&gt;
  requires Addable&lt;T&gt;</pre>
<pre>T sum(T a, T b){</pre>
<pre>   return a+b;</pre>
<pre>}</pre>
<p>In other words ConceptC++ puts restrictions on both template instantiation and template definition and binds them in a contract with respect to template parameter. This is what function, class or template definition, etc. are supposed to be.</p>
<p>We have been using magic words like CopyConstructable, Addable, etc. What are they? Are they new language features or are provided by library? What if we wanted to implement our own custom concepts? How will we go about that.</p>
<p>The C++ language addition provides capability to define new concepts while the library provides set of standard concepts. It is rather easy to build your own concept. For example let us look at definition of Addable concept:</p>
<pre><span style="color: #000000">auto concept Addable&lt;typename T&gt; {
  T operator+(T x, T y);
};</span></pre>
<p>How simple can that be? The above definition is saying that the Addable concept implies that the template parameter must implement operator+ which takes two parameters of type T and returns T.</p>
<p>With this background, let us modify out example sort template. In case of sort, the template parameters are iterators and value pointed to by the iterator must implement less than operator. With ConceptC++ you can specify these restrictions as follows:</p>
<pre><span style="color: #000000">#include &lt;concepts&gt;
template&lt;std::ForwardIterator T&gt;
    requires LessThanComparable&lt;T::value_type&gt;
</span></pre>
<pre><span style="color: #000000">void sort(T b, T e)</span></pre>
<pre><span style="color: #000000">{</span></pre>
<pre><span style="color: #000000">  ....</span></pre>
<pre><span style="color: #000000">}
</span></pre>
<p><span style="color: #000000">As you can see ConceptC++ provides natural way of definining restrictions on template parameters allowing compilers to perform early error checking.</span></p>
<pre></pre>
<h2><span style="color: #000000">Performance</span></h2>
<pre></pre>
<p><span style="color: #000000">The ConceptC++ implements its functionality by reducing compile speed. However, just like BCCL the ConceptC++ does not incur any run time overhead.<br />
</span></p>
<p><span style="color: #000000"><br />
</span></p>
<pre></pre>
<pre></pre>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/01/18/c0x-concepts-contd/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C++0x &#34;Concepts&#34;</title>
		<link>http://cppguru.techievarta.com/2009/01/17/c0x-concepts/</link>
		<comments>http://cppguru.techievarta.com/2009/01/17/c0x-concepts/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 05:41:54 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[c++0x]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ Reference]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[metaprogramming]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://cppguru.wordpress.com/?p=62</guid>
		<description><![CDATA[It is generally agreed that &#8220;Concept&#8221; is the biggest addition to C++ during C++0x (e.g. look @ Herb Shutter&#8217;s blog). The subject is large and I possibly cannot explain it in a single post. This post provides high level concept behind C++0x Concept.
Any person who has ever used C++ templates knows that compiler error messages [...]]]></description>
			<content:encoded><![CDATA[<p>It is generally agreed that &#8220;Concept&#8221; is the biggest addition to C++ during C++0x (e.g. look @ <a href="http://herbsutter.wordpress.com/2008/10/28/september-2008-iso-c-standards-meeting-the-draft-has-landed-and-a-new-convener/">Herb Shutter&#8217;s</a> blog). The subject is large and I possibly cannot explain it in a single post. This post provides high level concept behind C++0x Concept.</p>
<p>Any person who has ever used C++ templates knows that compiler error messages when using templates are pain to understand. The problem stands from the fact that there is no easy way for template developers to define constraints on template parameters. For example let us consider the following example:</p>
<pre><span style="font-size: small">#include &lt;vector&gt;
#include &lt;complex&gt;
#include &lt;algorithm&gt;

int main()
{
     std::vector&lt;std::complex&lt;float&gt; &gt; v;
     std::stable_sort(v.begin(), v.end());
}</span></pre>
<p>On my machine running GCC 4.2.1 it produces the following error:</p>
<blockquote><p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h: In function âvoid std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â:</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3176:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">t.cc:11:   instantiated from here</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:2356: error: no match for âoperator&lt;â in â__val &lt; __first. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]()â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h: In function âvoid std::__merge_without_buffer(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Distance, _Distance) [with _BidirectionalIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Distance = int]â:</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3182:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">t.cc:11:   instantiated from here</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3082: error: no match for âoperator&lt;â in â__middle. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]() &lt; __first. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]()â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h: In function âvoid std::__unguarded_linear_insert(_RandomAccessIterator, _Tp) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Tp = std::complex&lt;float&gt;]â:</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:2362:   instantiated from âvoid std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3176:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">t.cc:11:   instantiated from here</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:2309: error: no match for âoperator&lt;â in â__val &lt; __next. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]()â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h: In function â_ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&amp;) [with _ForwardIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Tp = std::complex&lt;float&gt;]â:</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3094:   instantiated from âvoid std::__merge_without_buffer(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Distance, _Distance) [with _BidirectionalIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Distance = int]â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3182:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</span></p>
<p><span style="font-size: small">t.cc:11:   instantiated from here</span></p>
<p><span style="font-size: small">&#8230;</span></p>
<p><span style="font-size: small">&#8230;</span></p>
<p><span style="font-size: small">/usr/include/c++/4.2.1/bits/stl_algo.h:3252: error: no match for âoperator&lt;â in â* __first2 &lt; * __first1â</span></p></blockquote>
<p>Experienced C++ programmers may be able to figure out this error message however for most users it is extremely difficult. The error message is long and does not provide any insight in to cause of the error. The basic error is that  std:complex&lt;float&gt; does not model LessThanComparable which is required by std:sort.  In C++ there is no easy way for template developers to provide requirements on their parameters. Hence, compilers are not capable of testing the parameter errors early and provide meaningful error message. This C++ deficiency has significantly affected wider adaptation of templates and has resulted in development various methods of providing template parameter checks. These alternatives include both Language based and library based. C++0x Concepts will provide language level facility for error early error detection and meaningful error generation for template parameters.</p>
<p>It will probably take some time before MSVC and G++ support Concepts in their mainline languages. Till such time, existing alternatives may provide you with short term solutions.</p>
<h2><a href="http://www.boost.org/doc/libs/1_37_0/libs/concept_check/concept_check.htm">Boost Concept Check Library</a></h2>
<p>The Boost Concept Checking Library provides a library level concepts check facility without any language changes. Also the mechanism does not incur any run-time overhead. The checks are completely implemented at compile time. According to BCCL, it provides:</p>
<blockquote>
<ul>
<li>A mechanism for inserting compile-time checks on template parameters     at their point of use.</li>
<li>A framework for specifying concept requirements though concept     checking classes.</li>
<li>A mechanism for verifying that concept requirements cover the     template.</li>
<li>A suite of concept checking classes and archetype classes that match     the concept requirements in the C++ Standard Library.</li>
<li>An alternative to the use of traits classes for accessing associated     types that mirrors the syntax proposed for the next C++ standard.</li>
</ul>
</blockquote>
<p>With BCCL the std::stable_sort can be written as:</p>
<pre>template&lt;typename T&gt;</pre>
<pre>void sort(T b, T e)</pre>
<pre>{</pre>
<pre>  function_requires&lt; LessThanComparableConcept&lt;T&gt; &gt;();</pre>
<pre>  typedef typename
     std::iterator_traits&lt;T&gt;::value_type value_type;</pre>
<pre>  function_requires
     &lt;LessThanComparableConcept&lt;value_type&gt; &gt;();</pre>
<pre>  ....</pre>
<pre>}</pre>
<p>With this change the error message changes to:</p>
<blockquote><p>/usr/include/boost/concept_check.hpp: In member function âvoid boost::LessThanComparableConcept&lt;TT&gt;::constraints() [with TT = std::complex&lt;float&gt;]â:</p>
<p>/usr/include/boost/concept_check.hpp:48:   instantiated from âvoid boost::function_requires(boost::mpl::identity&lt;T&gt;*) [with Concept = boost::LessThanComparableConcept&lt;std::complex&lt;float&gt; &gt;]â</p>
<p>t.cc:13:   instantiated from âvoid mysort(T, T) [with T = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:21:   instantiated from here</p>
<p><strong>/usr/include/boost/concept_check.hpp:314: error: no match for âoperator&lt;â in â((boost::LessThanComparableConcept&lt;std::complex&lt;float&gt; &gt;*)this)-&gt;boost::LessThanComparableConcept&lt;std::complex&lt;float&gt; &gt;::a &lt; ((boost::LessThanComparableConcept&lt;std::complex&lt;float&gt; &gt;*)this)-&gt;boost::LessThanComparableConcept&lt;std::complex&lt;float&gt; &gt;::bâ</strong></p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h: In function âvoid std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â:</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3176:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:15:   instantiated from âvoid mysort(T, T) [with T = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:21:   instantiated from here</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:2356: error: no match for âoperator&lt;â in â__val &lt; __first. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]()â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h: In function âvoid std::__merge_without_buffer(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Distance, _Distance) [with _BidirectionalIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Distance = int]â:</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3182:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:15:   instantiated from âvoid mysort(T, T) [with T = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:21:   instantiated from here</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3082: error: no match for âoperator&lt;â in â__middle. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]() &lt; __first. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]()â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h: In function âvoid std::__unguarded_linear_insert(_RandomAccessIterator, _Tp) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Tp = std::complex&lt;float&gt;]â:</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:2362:   instantiated from âvoid std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3176:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:15:   instantiated from âvoid mysort(T, T) [with T = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:21:   instantiated from here</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:2309: error: no match for âoperator&lt;â in â__val &lt; __next. __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = std::complex&lt;float&gt;*, _Container = std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt;]()â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h: In function â_ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&amp;) [with _ForwardIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Tp = std::complex&lt;float&gt;]â:</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3094:   instantiated from âvoid std::__merge_without_buffer(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Distance, _Distance) [with _BidirectionalIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;, _Distance = int]â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3182:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3892:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:15:   instantiated from âvoid mysort(T, T) [with T = __gnu_cxx::__normal_iterator&lt;std::complex&lt;float&gt;*, std::vector&lt;std::complex&lt;float&gt;, std::allocator&lt;std::complex&lt;float&gt; &gt; &gt; &gt;]â</p>
<p>t.cc:21:   instantiated from here</p>
<p>&#8230;.<br />
&#8230;.</p>
<p>/usr/include/c++/4.2.1/bits/stl_algo.h:3252: error: no match for âoperator&lt;â in â* __first2 &lt; * __first1â</p></blockquote>
<p>Clearly a better error message (still a little longer than what we want it to be).</p>
<p>Although, BCCL provides better error message. The biggest drawback of the BCCL is that the restriction on the template parameter is not part of template prototype and hence not easily discoverable by algorithm user.  Language level support is needed to support that. The ConceptC++ was an experimental implementation of language level support. Significant parts of C++0x is derived from ConceptC++ and boost experimantation. I will discuss these changes in future posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/01/17/c0x-concepts/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C++ Proxy Template</title>
		<link>http://cppguru.techievarta.com/2009/01/14/c-proxy-template/</link>
		<comments>http://cppguru.techievarta.com/2009/01/14/c-proxy-template/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 03:15:32 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[templates]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ Reference]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://cppguru.wordpress.com/?p=66</guid>
		<description><![CDATA[Earlier we had discussed Java Proxy class and had looked for ways to develop similar facility with C++. It turns out there is a rather easy way to provide such a facility. If you have used smart pointers, you have already used one form C++ proxy template. Smart pointers essentially provide a proxy facility. Bjarne [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cppguru.isa-geek.com/2009/01/09/javastyledynamicproxyin/">Earlier we had discussed</a> Java Proxy class and had looked for ways to develop similar facility with C++. It turns out there is a rather easy way to provide such a facility. If you have used smart pointers, you have already used one form C++ proxy template. Smart pointers essentially provide a proxy facility. Bjarne Stroustrup covered this subject in <a href="http://www.research.att.com/~bs/wrapper.pdf">this paper</a>.</p>
<p>The key to the wrapper definition is that the fact that all objects created on the stack are eventually destroyed when the object goes out of the scope. For example:</p>
<pre>class Test{
    void method(){
         X a;
       // Implementation
       return;
     }

}</pre>
<p>In the above case the X&#8217;s destructor is called when Test::method returns due to return call or exception.</p>
<p>Now let us use this knowledge to define the Wrap class. Let us first define the prefix, suffix and the wrapped class:</p>
<pre>#include &lt;iostream&gt;
 using namespace std;
 void prefix() { cout &lt;&lt; "prefix\n" }
 void suffix() { cout &lt;&lt; "suffix\n"; }</pre>
<p>// The person class looks like this:</p>
<pre>class Person{
  std::string mName;
  Person(std::string pName): mName(name){}
  void printName(){
     std::cout &lt;&lt; mName &lt;&lt; std::endl;
  }

};</pre>
<p>Now we can define the Wrap class as the following:</p>
<pre>template &lt;class T &gt;
class Wrap {
     T * p ;
     public:
             Wrap (T * pp ) :p (pp) { }
             Call_proxy &lt;T&gt; operator -&gt;() {
                   prefix ();
                   return Call_proxy&lt;T&gt;(p);
             }
};</pre>
<pre>template &lt;class T &gt;
class Call_proxy {
       T * p ;
       public :
              Call_proxy (T * pp ) :p (pp ){ }
              ˜Call_proxy () {
                     suffix ();
               }
               T * operator -&gt;() {
                          return p ;
                }
 };</pre>
<p>We can now wrap any object  of type T in the Wrap class. For example: we can take class Person and wrap it like Wrap&lt;Person&gt; person(new Person);</p>
<p>Any dereferencce of person using -&gt; operator, results in call to operator-&gt; of Wrap class. The method calls the prefix function , creates a new object of type Call_proxy containing the pointer to object. The method is called. On return from the method, Call_proxy is destroyed which implicitly results in call to suffix().</p>
<p>Let us test it. the following code snippet:</p>
<pre>Wrap&lt;Person&gt; person(new Person("test"));
person-&gt;printName();</pre>
<p>should print:</p>
<pre>prefix
test
suffix</pre>
<p>Rather straightforward and elegant to use. But, syntatic sugar to implement.</p>
<p>Note that suffix will be called even if the method returned due to exception.  This is better than Java, where the programmer has to take explicit action to make sure suffix is called in case of exception.</p>
<p>The Stroustrup&#8217;s paper also covers other issues like ownership, parametrization etc.  In most cases you may not need to worry about those. If you need to, take a look at the paper.</p>
<p>Happy proxying!</p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/01/14/c-proxy-template/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Template or not to Template</title>
		<link>http://cppguru.techievarta.com/2009/01/10/template-or-not-to-template/</link>
		<comments>http://cppguru.techievarta.com/2009/01/10/template-or-not-to-template/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 18:13:45 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[templates]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[C++ tutorial]]></category>

		<category><![CDATA[metaprogramming]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://cppguru.wordpress.com/?p=52</guid>
		<description><![CDATA[As I was learning C++ over an year ago,  I boughtModern C++ Design: Generic Programming and Design Patterns Applied (C++ In-Depth Series). As I read through the book, I was simply amazed by power offered by templates. Experience with the book was followed by Boost libraries. Specifically boost lambda and what it could provide to [...]]]></description>
			<content:encoded><![CDATA[<p>As I was learning C++ over an year ago,  I boughtModern C++ Design: Generic Programming and Design Patterns Applied (C++ In-Depth Series). As I read through the book, I was simply amazed by power offered by templates. Experience with the book was followed by Boost libraries. Specifically boost lambda and what it could provide to programmer. These went way beyond traditional STL use of templates. This excitement with C++ templates was followed by my experimentation with using templates in my own projects.  This post summarizes  experiences with templates.</p>
<ul>
<li>Templates provide power with efficiency: Templates provide a lot of power without compromising efficiency. The Andrei&#8217;s book is full of examples of such power. A quick look inside the boost library provides examples of such power. Look at simple construct like <a href="http://www.boost.org/doc/libs/1_37_0/doc/html/any.html">boost::any</a> which allows you to hold any type of data in it so that you can create a container/array which can hold boolean, integers, your various classes, etc. all at the same time. You can also look at a lot more complex construct like boost::lambda library which brings lambda programming to C++. Recently the boost has added boost::proto discussed in a seperate post on this site. The power really exists. Using these libraries can make your programming life a lot simpler. It is natural tendency to write your own templates on the same line. Should you or shouldn&#8217;t you?</li>
<li>Templates are not well understood: The templates are not well understood yet. Most experienced C++ programmers find it hard to understand how to program templates. At present templates are a the stage as Object Oriented Programming was several years ago. The compilers have progressed to state where they can correctly compile templates. However, the power of templates is yet to be distilled in to developer community at large. So if you decide to use templates, please be patient and give plenty of time for developers to learn and get up to speed.</li>
<li>Template debugging is horrifying experience for first time users: Error messages produced by compiler are horrifying large and complex. For simplest of errors, one gets 3 pages long error message with actual error who knows where. Once you get used to of debugging compilation issues you can generally figure out the error but it takes time to get used to of those errors. Template adaption will go a long way if the compilers start producing better error messages. But if you think compiler was the only issue just wait till you get to debuggers. The debuggers have not yet found a good way to deal with templates. Last but not least is compile time with templates. With most compilers, templates have to be included in header files. A simple change in the template requires compilation of all of your code which uses templates. This is like going backward on libraries.</li>
</ul>
<p>In my experience, templates offer a lot of power and flexibility without compromising efficiency. The compilers can mostly deal with templates correctly. However debugger, compiler errors, and ways to reduce compile time can go a very long way in helping wider adaptation of templates. But if you are one of those early adapters and technology enthusiast, go ahead experiment and adapt templates and it will go a long way in helping your projects.</p>
<p>If you are going to use templates, you should start off by reading <a href="http://www.parashift.com/c++-faq-lite/templates.html">C++ FAQ Lite on template</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/01/10/template-or-not-to-template/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Java Style Dynamic Proxy in C++</title>
		<link>http://cppguru.techievarta.com/2009/01/09/javastyledynamicproxyin/</link>
		<comments>http://cppguru.techievarta.com/2009/01/09/javastyledynamicproxyin/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 04:49:51 +0000</pubDate>
		<dc:creator>Umesh</dc:creator>
		
		<category><![CDATA[c++0x]]></category>

		<category><![CDATA[boost]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://cppguru.wordpress.com/?p=49</guid>
		<description><![CDATA[This post defines C++ template based implementation for C++ proxy.
I recently came across Java&#8217;s proxy object. That is some cool technology. Using this one can wrap one object in another object in a completely typesafe way. From user&#8217;s point of view, the proxy object looks something like:
ProxiedObjectInterface obj = (ProxiedObjectInterface)ProxyCreator.newInstance( proxiedObject );
More details on proxy [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em><a href="http://cppguru.wordpress.com/2009/01/14/c-proxy-template/">This post</a> defines C++ template based implementation for C++ proxy.</em></strong></p>
<p>I recently came across Java&#8217;s proxy object. That is some cool technology. Using this one can wrap one object in another object in a completely typesafe way. From user&#8217;s point of view, the proxy object looks something like:</p>
<p>ProxiedObjectInterface obj = (ProxiedObjectInterface)ProxyCreator.newInstance( proxiedObject );</p>
<p>More details on <a href="http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html">proxy object are available here.</a></p>
<p>Since the interface exposed is of the proxied type, all calls to obj are typsef and can be checked by compiler. In java ProxyCreator class does not need to know anything about proxied object. This makes it very powerful.</p>
<p>I have been wondering if there is any easy way to create a similar facility in C++. I cannot think of one right away. I know we can do some cool things with template meta-programming. What will that be?</p>
]]></content:encoded>
			<wfw:commentRss>http://cppguru.techievarta.com/2009/01/09/javastyledynamicproxyin/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
