<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

    <title>TIL</title>
    <link href="http://akhodakivskiy.github.com/atom.xml" rel="self"/>
    <link href="http://akhodakivskiy.github.com/"/>
    <updated>2013-07-21T13:42:33-07:00</updated>
    <id>http://akhodakivskiy.github.com/</id>
    <author>
        <name>Anton Khodakivskiy</name>
        <email>akhodakivskiy@gmail.com</email>
    </author>

    
    <entry>
        <title>Forward Declaration and Private Implementation in C++</title>
        <link href="http://akhodakivskiy.github.com/2012/12/11/private-implementation-forward-declaration.html"/>
        <updated>2012-12-11T00:00:00-08:00</updated>
        <id>http://akhodakivskiy.github.com/2012/12/11/private-implementation-forward-declaration</id>
        <content type="html">&lt;div class='row-fluid'&gt;
    &lt;div class='span6'&gt;
&lt;p&gt;Well&amp;#8230; I catually learned this a long time ago, but nevertheless.&lt;/p&gt;

&lt;p&gt;Includes in C/C++ are extremely inefficient. It&amp;#8217;s not uncommon to find a system where a single change to a header file file would cause 10 minutes recompilation. I used to suffer from this a lot. I could count the number of times my code had compiled (or not) in a day by the number of tea/coffee cups consumed plus the number of articles read.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s been a lot of discussion on C++ modules recently. &lt;a href='http://llvm.org/devmtg/2012-11/Gregor-Modules.pdf'&gt;These slides by Doug Gregor&lt;/a&gt; explain what&amp;#8217;s happening under the hood, why C++ compilation tends to be so slow, and how C++ modules can help. The bottom line is that compiling even a single source file might take a while.&lt;/p&gt;
&lt;/div&gt;
    &lt;div class='span6'&gt;
        

&lt;div class='thumbnail centered-text'&gt;
&lt;p&gt;&lt;img alt='Its Compiling' src='http://imgs.xkcd.com/comics/compiling.png' /&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;a href='http://xkcd.com/303/'&gt;XKCD #303&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;

&lt;/div&gt;


    &lt;/div&gt;
&lt;/div&gt;&lt;!-- more --&gt;
&lt;h4 id='whats_the_big_deal'&gt;What&amp;#8217;s the big deal?&lt;/h4&gt;

&lt;p&gt;In a typical coding routine one might make a few changes to a single file and then check if the software still compiles. Changing a &lt;code&gt;.cpp&lt;/code&gt; implementation file won&amp;#8217;t take much time to recompile. But things get worse if a &lt;code&gt;.h&lt;/code&gt; header is changed. In this case all the implementation files that directly or indirectly include the header in question will require a round of recompilation (&lt;code&gt;three.h&lt;/code&gt; might include &lt;code&gt;two.h&lt;/code&gt; indirectly including &lt;code&gt;one.h&lt;/code&gt;). A small change might result in recompiling hundreds of files. And most of these files wouldn&amp;#8217;t use anything from the modified header.&lt;br /&gt;Time to get another coffee, read an article, or engage in a sword fight with your colleague.&lt;/p&gt;

&lt;p&gt;While it&amp;#8217;s hard to make GCC compile stuff faster, there are a few things that can be done to optimize the compilation, namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid including headers using &lt;a href='http://en.wikipedia.org/wiki/Forward_declaration'&gt;Forward Declaration&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Avoid changing headers using &lt;a href='http://en.wikipedia.org/wiki/Private_class_data_pattern'&gt;Private Implementation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id='fdecl'&gt;Avoid including headers (Forward Declaration)&lt;/h4&gt;

&lt;p&gt;Consider the following code:&lt;/p&gt;
&lt;div class='row-fluid'&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt; 1&lt;/span&gt; &lt;span class='cm'&gt;/* one.h */&lt;/span&gt;
&lt;span class='lineno'&gt; 2&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;One&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt; 3&lt;/span&gt;     &lt;span class='k'&gt;public&lt;/span&gt;&lt;span class='o'&gt;:&lt;/span&gt;
&lt;span class='lineno'&gt; 4&lt;/span&gt;         &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt; 5&lt;/span&gt;     &lt;span class='p'&gt;...&lt;/span&gt; 
&lt;span class='lineno'&gt; 6&lt;/span&gt; &lt;span class='p'&gt;};&lt;/span&gt;
&lt;span class='lineno'&gt; 7&lt;/span&gt; 
&lt;span class='lineno'&gt; 8&lt;/span&gt; &lt;span class='cm'&gt;/* one.cpp */&lt;/span&gt;
&lt;span class='lineno'&gt; 9&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;one.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt;10&lt;/span&gt; &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;One&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt;11&lt;/span&gt;     &lt;span class='k'&gt;return&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt; 
&lt;span class='lineno'&gt;12&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt; 1&lt;/span&gt; &lt;span class='cm'&gt;/* two.h */&lt;/span&gt;
&lt;span class='lineno'&gt; 2&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;one.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt; 3&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Two&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt;
&lt;span class='lineno'&gt; 4&lt;/span&gt;     &lt;span class='nl'&gt;public:&lt;/span&gt;
&lt;span class='lineno'&gt; 5&lt;/span&gt;         &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;one&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt; 6&lt;/span&gt;         &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='nf'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt; 7&lt;/span&gt;     &lt;span class='p'&gt;...&lt;/span&gt;
&lt;span class='lineno'&gt; 8&lt;/span&gt;     &lt;span class='nl'&gt;private:&lt;/span&gt;
&lt;span class='lineno'&gt; 9&lt;/span&gt;         &lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt;10&lt;/span&gt; &lt;span class='p'&gt;};&lt;/span&gt;
&lt;span class='lineno'&gt;11&lt;/span&gt; 
&lt;span class='lineno'&gt;12&lt;/span&gt; &lt;span class='cm'&gt;/* two.cpp */&lt;/span&gt;
&lt;span class='lineno'&gt;13&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;two.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt;14&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;one&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='o'&gt;:&lt;/span&gt; &lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;one&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;span class='lineno'&gt;15&lt;/span&gt; 
&lt;span class='lineno'&gt;16&lt;/span&gt; &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt;17&lt;/span&gt;     &lt;span class='k'&gt;return&lt;/span&gt; &lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='o'&gt;-&amp;gt;&lt;/span&gt;&lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt; 
&lt;span class='lineno'&gt;18&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this example changing the header &lt;code&gt;one.h&lt;/code&gt; would cause recompilation of all the source files that directly or indirectly include both &lt;code&gt;one.h&lt;/code&gt; and &lt;code&gt;two.h&lt;/code&gt;. If &lt;code&gt;two.h&lt;/code&gt; is an important header and has lots of dependents then all this sub tree will require a round of recompilation.&lt;/p&gt;

&lt;p&gt;But there is no need to include &lt;code&gt;one.h&lt;/code&gt; from &lt;code&gt;two.h&lt;/code&gt; because the definition of &lt;code&gt;class Two&lt;/code&gt; doesn&amp;#8217;t need to know anything about the definition of &lt;code&gt;class One&lt;/code&gt;. It&amp;#8217;s enough to know that the constructor argument &lt;code&gt;One *one&lt;/code&gt; is a pointer to an instance of a class&amp;#8230; some class defined later on. Thus we can forward declare &lt;code&gt;class One&lt;/code&gt; and move &lt;code&gt;#include one.h&lt;/code&gt; into the the implementation file &lt;code&gt;two.cpp&lt;/code&gt;:&lt;/p&gt;
&lt;div class='row-fluid'&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt; 1&lt;/span&gt; &lt;span class='cm'&gt;/* two.h */&lt;/span&gt;
&lt;span class='lineno'&gt; 2&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;One&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt; 3&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Two&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt;
&lt;span class='lineno'&gt; 4&lt;/span&gt;     &lt;span class='nl'&gt;public:&lt;/span&gt;
&lt;span class='lineno'&gt; 5&lt;/span&gt;         &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;one&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt; 6&lt;/span&gt;         &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='nf'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt; 7&lt;/span&gt;     &lt;span class='p'&gt;...&lt;/span&gt;
&lt;span class='lineno'&gt; 8&lt;/span&gt;     &lt;span class='nl'&gt;private:&lt;/span&gt;
&lt;span class='lineno'&gt; 9&lt;/span&gt;         &lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt;10&lt;/span&gt; &lt;span class='p'&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt;1&lt;/span&gt; &lt;span class='cm'&gt;/* two.cpp */&lt;/span&gt;
&lt;span class='lineno'&gt;2&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;two.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt;3&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;one.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt;4&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;one&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='o'&gt;:&lt;/span&gt; &lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;one&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;span class='lineno'&gt;5&lt;/span&gt; 
&lt;span class='lineno'&gt;6&lt;/span&gt; &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt;7&lt;/span&gt;     &lt;span class='k'&gt;return&lt;/span&gt; &lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='o'&gt;-&amp;gt;&lt;/span&gt;&lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt; 
&lt;span class='lineno'&gt;8&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this setup we avoid recompilation of all the dependents of &lt;code&gt;two.h&lt;/code&gt;, which may or may not be a lot of CPU cycles. If it&amp;#8217;s not a lot of work - then good for you, your code base is probably well decoupled.&lt;br /&gt;But quite often this simple technique will save you a lot of time.&lt;/p&gt;

&lt;h4 id='pimpl'&gt;Avoid changing headers (Private Implementation)&lt;/h4&gt;

&lt;p&gt;Lets consider a similar but slightly different case:&lt;/p&gt;
&lt;div class='row-fluid'&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt; 1&lt;/span&gt; &lt;span class='cm'&gt;/* one.h */&lt;/span&gt;
&lt;span class='lineno'&gt; 2&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;One&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt; 3&lt;/span&gt;     &lt;span class='k'&gt;public&lt;/span&gt;&lt;span class='o'&gt;:&lt;/span&gt;
&lt;span class='lineno'&gt; 4&lt;/span&gt;         &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt; 5&lt;/span&gt;     &lt;span class='p'&gt;...&lt;/span&gt; 
&lt;span class='lineno'&gt; 6&lt;/span&gt; &lt;span class='p'&gt;};&lt;/span&gt;
&lt;span class='lineno'&gt; 7&lt;/span&gt; 
&lt;span class='lineno'&gt; 8&lt;/span&gt; &lt;span class='cm'&gt;/* one.cpp */&lt;/span&gt;
&lt;span class='lineno'&gt; 9&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;one.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt;10&lt;/span&gt; &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;One&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt;11&lt;/span&gt;     &lt;span class='k'&gt;return&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt; 
&lt;span class='lineno'&gt;12&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt; 1&lt;/span&gt; &lt;span class='cm'&gt;/* two.h */&lt;/span&gt;
&lt;span class='lineno'&gt; 2&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;one.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt; 3&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Two&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt;
&lt;span class='lineno'&gt; 4&lt;/span&gt;     &lt;span class='nl'&gt;public:&lt;/span&gt;
&lt;span class='lineno'&gt; 5&lt;/span&gt;         &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt; 6&lt;/span&gt;     &lt;span class='p'&gt;...&lt;/span&gt;
&lt;span class='lineno'&gt; 7&lt;/span&gt;     &lt;span class='nl'&gt;private:&lt;/span&gt;
&lt;span class='lineno'&gt; 8&lt;/span&gt;         &lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt; 9&lt;/span&gt; &lt;span class='p'&gt;};&lt;/span&gt;
&lt;span class='lineno'&gt;10&lt;/span&gt; 
&lt;span class='lineno'&gt;11&lt;/span&gt; &lt;span class='cm'&gt;/* two.cpp */&lt;/span&gt;
&lt;span class='lineno'&gt;12&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;two.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt;13&lt;/span&gt; &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt;14&lt;/span&gt;     &lt;span class='k'&gt;return&lt;/span&gt; &lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;.&lt;/span&gt;&lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt; 
&lt;span class='lineno'&gt;15&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this case &lt;code&gt;class One&lt;/code&gt; is an instance member of &lt;code&gt;class Two&lt;/code&gt;. Here the compiler has to know the definition of &lt;code&gt;class Two&lt;/code&gt; in order to include an instance as a member of &lt;code&gt;class One&lt;/code&gt;. Thus &lt;code&gt;two.h&lt;/code&gt; has to include &lt;code&gt;one.h&lt;/code&gt;&amp;#8230;&lt;/p&gt;

&lt;p&gt;Unless all the private business of &lt;code&gt;class Two&lt;/code&gt; is hid using the Private Implementation pattern! Lets reorganize &lt;code&gt;two.h&lt;/code&gt; and &lt;code&gt;two.cpp&lt;/code&gt; a little bit:&lt;/p&gt;
&lt;div class='row-fluid'&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt; 1&lt;/span&gt; &lt;span class='cm'&gt;/* two.h */&lt;/span&gt;
&lt;span class='lineno'&gt; 2&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;TwoPrivate&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt; 3&lt;/span&gt; 
&lt;span class='lineno'&gt; 4&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Two&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt;
&lt;span class='lineno'&gt; 5&lt;/span&gt;     &lt;span class='nl'&gt;public:&lt;/span&gt;
&lt;span class='lineno'&gt; 6&lt;/span&gt;         &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;();&lt;/span&gt;
&lt;span class='lineno'&gt; 7&lt;/span&gt;         &lt;span class='o'&gt;~&lt;/span&gt;&lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;();&lt;/span&gt;
&lt;span class='lineno'&gt; 8&lt;/span&gt; 
&lt;span class='lineno'&gt; 9&lt;/span&gt;         &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='nf'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt;10&lt;/span&gt;     &lt;span class='p'&gt;...&lt;/span&gt;
&lt;span class='lineno'&gt;11&lt;/span&gt;     &lt;span class='nl'&gt;private:&lt;/span&gt;
&lt;span class='lineno'&gt;12&lt;/span&gt;         &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='k'&gt;const&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt; &lt;span class='o'&gt;&amp;amp;&lt;/span&gt;&lt;span class='n'&gt;two&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt;13&lt;/span&gt;         &lt;span class='n'&gt;Two&lt;/span&gt; &lt;span class='o'&gt;&amp;amp;&lt;/span&gt;&lt;span class='k'&gt;operator&lt;/span&gt;&lt;span class='o'&gt;=&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='k'&gt;const&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt; &lt;span class='o'&gt;&amp;amp;&lt;/span&gt;&lt;span class='n'&gt;two&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt;
&lt;span class='lineno'&gt;14&lt;/span&gt;         &lt;span class='n'&gt;TwoPrivate&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;d&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt;15&lt;/span&gt; &lt;span class='p'&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
    &lt;div class='span6'&gt;
        
&lt;div class='panel'&gt;

&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='cpp'&gt;&lt;span class='lineno'&gt; 1&lt;/span&gt; &lt;span class='cm'&gt;/* two.cpp */&lt;/span&gt;
&lt;span class='lineno'&gt; 2&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;two.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt; 3&lt;/span&gt; &lt;span class='cp'&gt;#include &amp;quot;one.h&amp;quot;&lt;/span&gt;
&lt;span class='lineno'&gt; 4&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;TwoPrivate&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt;
&lt;span class='lineno'&gt; 5&lt;/span&gt;     &lt;span class='nl'&gt;public:&lt;/span&gt;
&lt;span class='lineno'&gt; 6&lt;/span&gt;         &lt;span class='n'&gt;TwoPrivate&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;Two&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;owner&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; 
&lt;span class='lineno'&gt; 7&lt;/span&gt;             &lt;span class='o'&gt;:&lt;/span&gt; &lt;span class='n'&gt;m_Owner&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;onwer&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{}&lt;/span&gt;
&lt;span class='lineno'&gt; 8&lt;/span&gt;         &lt;span class='o'&gt;~&lt;/span&gt;&lt;span class='n'&gt;TwoPrivate&lt;/span&gt;&lt;span class='p'&gt;()&lt;/span&gt; &lt;span class='p'&gt;{}&lt;/span&gt;
&lt;span class='lineno'&gt; 9&lt;/span&gt; 
&lt;span class='lineno'&gt;10&lt;/span&gt;         &lt;span class='n'&gt;One&lt;/span&gt; &lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt;11&lt;/span&gt;     &lt;span class='nl'&gt;private:&lt;/span&gt;
&lt;span class='lineno'&gt;12&lt;/span&gt;         &lt;span class='n'&gt;Two&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;m_Owner&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt;
&lt;span class='lineno'&gt;13&lt;/span&gt; &lt;span class='p'&gt;};&lt;/span&gt;
&lt;span class='lineno'&gt;14&lt;/span&gt; 
&lt;span class='lineno'&gt;15&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;()&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; &lt;span class='n'&gt;d&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='k'&gt;new&lt;/span&gt; &lt;span class='n'&gt;TwoPrivate&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='k'&gt;this&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;span class='lineno'&gt;16&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::~&lt;/span&gt;&lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='p'&gt;()&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; &lt;span class='k'&gt;delete&lt;/span&gt; &lt;span class='n'&gt;d&lt;/span&gt;&lt;span class='p'&gt;;&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;span class='lineno'&gt;17&lt;/span&gt; 
&lt;span class='lineno'&gt;18&lt;/span&gt; &lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;Two&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='n'&gt;squareUsingOne&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;int&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt; 
&lt;span class='lineno'&gt;19&lt;/span&gt;     &lt;span class='k'&gt;return&lt;/span&gt; &lt;span class='n'&gt;d&lt;/span&gt;&lt;span class='o'&gt;-&amp;gt;&lt;/span&gt;&lt;span class='n'&gt;m_One&lt;/span&gt;&lt;span class='p'&gt;.&lt;/span&gt;&lt;span class='n'&gt;square&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='p'&gt;);&lt;/span&gt; 
&lt;span class='lineno'&gt;20&lt;/span&gt; &lt;span class='p'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What happened here? A few things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;class Two&lt;/code&gt; doesn&amp;#8217;t have &lt;code&gt;class One&lt;/code&gt; as an instance member anymore,&lt;/li&gt;

&lt;li&gt;&lt;code&gt;class Two&lt;/code&gt; now has a pointer member to &lt;code&gt;class TwoPrivate&lt;/code&gt;. It&amp;#8217;s &lt;a href='#fdecl'&gt;forward declared&lt;/a&gt; because we only deal with a pointer,&lt;/li&gt;

&lt;li&gt;&lt;code&gt;class Two&lt;/code&gt; has disabled copy constructor and assignment operator in order to &lt;a href='http://www.reddit.com/r/programming/comments/14no5v/forward_declaration_and_private_implementation_in/c7ey70s'&gt;avoid memory leaks and double deletion&lt;/a&gt;.&lt;/li&gt;

&lt;li&gt;&lt;code&gt;class TwoPrivate&lt;/code&gt; is instantiated and destroyed along with &lt;code&gt;class Two&lt;/code&gt; and is accessible via the &lt;code&gt;TwoPrivate *d&lt;/code&gt; pointer,&lt;/li&gt;

&lt;li&gt;&lt;code&gt;class One&lt;/code&gt; is now an instance member of &lt;code&gt;class TwoPrivate&lt;/code&gt;, accessible in &lt;code&gt;class Two&lt;/code&gt; members via &lt;code&gt;d-&amp;gt;m_One&lt;/code&gt;,&lt;/li&gt;

&lt;li&gt;Consequently the troubling &lt;code&gt;#include &amp;quot;one.h&amp;quot;&lt;/code&gt; has been moved from the header &lt;code&gt;two.h&lt;/code&gt; to the implementation &lt;code&gt;two.cpp&lt;/code&gt; slashing a branch of the inclusion tree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are quite a few benefits from using this pattern. To name a few&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The implementation of the class that uses private implementation doesn&amp;#8217;t affect the interface at all! Members and methods can now be added and removed, and only the &lt;code&gt;.cpp&lt;/code&gt; implementation file will require recompilation,&lt;/li&gt;

&lt;li&gt;The header is now much cleaner - without all the private litter,&lt;/li&gt;

&lt;li&gt;It&amp;#8217;s easier to maintain binary compatibility. If you work on a dll that&amp;#8217;s called by third party application, then one can safely change the implementation, add/remove private members and methods, and have an interface that&amp;#8217;s compatible with previous versions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There some drawback as well&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is a slight increase in memory allocation - from 12 bytes per object depending on the platform.&lt;/li&gt;

&lt;li&gt;Everything that belongs to the Private Implementation will be allocated on the heap. Heap allocation is usually much more expensive than stack allocation. Frequent and long lasting heap allocations often lead to dramatic memory framentation further decreasing performance.&lt;/li&gt;

&lt;li&gt;There is additional pointer indirection every time Private Implementation member or method is accessed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Private Implementation alone is an extended topic. The solution in this article has many flaws, but it&amp;#8217;s probably the simplest implementation possible that gives more or less clear idea of the concept. Proper solution that is at the same time correct, consice, and elegant probably just doesn&amp;#8217;t exist. There is &lt;a href='http://loki-lib.cvs.sourceforge.net/viewvc/loki-lib/loki/include/loki/Pimpl.h?view=markup'&gt;Loki Pimpl&lt;/a&gt; class that&amp;#8217;s takes care of all the edge cases. It&amp;#8217;s really a matter of taste and preference. Use the correct but complex code or the simple but flawed in some way? Use Private Implementation or not to use it at all?&lt;/p&gt;

&lt;h4 id='conclusion'&gt;Conclusion&lt;/h4&gt;

&lt;p&gt;Forward Declaration and Private Implementation can significantly reduce header dependencies within a C++ project, save a lot of CPU cycles avoiding unnecessary recompilation, and make your code a lot cleaner.&lt;/p&gt;</content>
    </entry>
    
    <entry>
        <title>Hello World</title>
        <link href="http://akhodakivskiy.github.com/2012/12/10/hello-world.html"/>
        <updated>2012-12-10T00:00:00-08:00</updated>
        <id>http://akhodakivskiy.github.com/2012/12/10/hello-world</id>
        <content type="html">&lt;p&gt;The time has come for me to venture into the blogging business. Every day I deal with various software related conecpts, ideas, and problems. I learn from these interactions a lot. What bothers me is that now, after 6 years of professional software development I realize that I don&amp;#8217;t really remmber what I&amp;#8217;ve been working on in the past years. There are lots of grey areas even if I sit down and revisit some of my past code. In other words the retention isn&amp;#8217;t that great.&lt;/p&gt;

&lt;p&gt;In this blog I&amp;#8217;m going to regularly write on the aforementioned topics. And I hope that I will get better idea about them after formalizing my findings in writing.&lt;/p&gt;

&lt;p&gt;For this purpose I set up this blog. It was a challenge of its own to make a platform choice. Nowardays there is a ton of different hosted blogging platforms - Wordpress, Blogger, Tubmblr to name a few. But I happened to stumble upon &lt;a href='http://pages.github.com/'&gt;GiHub:Pages&lt;/a&gt; and &lt;a href='https://github.com/mojombo/jekyll'&gt;Jekyll&lt;/a&gt;. This neat combo currently drives my blog.&lt;/p&gt;

&lt;p&gt;Jekyll is a blow aware static site generator. It runs on a bunch of templated files and outputs static HTML which can be hosted virtually anywhere, and on GitHub:Pages in particular. GitHub:Pages will run Jekyll begind the scenes, and serve the generated site. My job was to set up all the templates and configuration. There is a few prebuilt solutions available for this purpose - &lt;a href='http://jekyllbootstrap.com/'&gt;Jekyll-Bootstrap&lt;/a&gt; or &lt;a href='https://github.com/imathis/octopress/'&gt;Octopress&lt;/a&gt;, but I wanted to go throught this process myself, and build it from scratch. Of course my implementation doesn&amp;#8217;t have too many features, but it&amp;#8217;s mine :)&lt;/p&gt;

&lt;p&gt;In a few words - Jekyll grabs HTML, Markdown, or Textile files, passes them through the specified template and outputs a plain HTML file. The beauty of this approach is that I can write posts in my favorite editor - Vim and just push them to my GitHub repo. GitHub:Pages will then call Jekyll on the collection of files, regenerate the site and promptly publish it.&lt;/p&gt;

&lt;p&gt;Such a blog is blazing fast, the hosting is free, and the amount of control over the content and markup is just incredible. And &lt;a href='http://twitter.github.com/bootstrap/'&gt;Twitter Bootstrap&lt;/a&gt; has everything a wannabe gloggers needs to create pretty posts.&lt;/p&gt;

&lt;p&gt;The source code for this blog &lt;a href='https://github.com/akhodakivskiy/akhodakivskiy.github.com'&gt;rests here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope that wasn&amp;#8217;t too bad for the first time :)&lt;/p&gt;</content>
    </entry>
    

</feed>
