Generic programming to fight the rigidity in the C++ projects
This is an interesting write-up, but it leaves out some specifics in designing C++. A few are:<p>1. Concurrency<p>In garbage collected languages, the runtime can generally add objects to a free list and clean up memory when it&#x27;s more-or-less convenient. Since C++ uses deterministic destruction and freeing, there has to be clear responsibilities defined. In particular, it has to be clear which thread is ultimately responsible for running the destructor. Point being, writing code that would otherwise be &quot;abstract&quot; in Java-land implies (but rarely explicitly defines!) some rigidity in design with respect to how long objects need to be around for different threads to use them.<p>Maybe that&#x27;s too abstract. Here&#x27;s some code to illustrate:<p><pre><code>    class IHandler {
    public:
        virtual Status handleAsync(const Msg &amp; msg) = 0
    };

    class CopyingHandler : public IHandler {
    private:
        WorkerThread m_worker;

    public:
        Status handleAsync(const Msg &amp; msg) override {
            &#x2F;&#x2F; capture the msg by value, taking a copy
            return m_worker.push_back([=msg](){
                handle(msg);
            });
        }
    };

    class RefHandler : public IHandler {
    private:
        WorkerThread m_worker;

    public:
        Status handleAsync(const Msg &amp; msg) override {
            &#x2F;&#x2F; Capture the msg by reference. Assumes
            &#x2F;&#x2F; sticks around at least as long as the
            &#x2F;&#x2F; background thread does.
            return m_worker.push_back([&amp;msg](){
                handle(msg);
            });
        }
    };
</code></pre>
Anyway, I left some details out, but you can see that both implementations adhere to the interface provided by IHandler, but each makes drastically different assumptions about what&#x27;s OK. In some cases, problem domains, copying the message might create a performance bug. In most programs, you could easily (likely) have undefined behavior by capturing the message by reference, letting the message be destroyed, and then trying to use the message afterwards.<p>Point being, rigidity in C++ designs is easy to create subtly and accidentally, and even generic programming won&#x27;t necessarily help you here.<p>2. Polymorphism<p>C++ actually allows for quite a range of polymorphic behaviors. You can&#x27;t really rank them from highest rigidity to the least, since there are pros and cons to each approach, but generally inheritance is more rigid than the other approaches. In contrast, you can also have:<p>2a. Opaque Pointers<p>Not necessarily very type safe, but if you need to wrap something up in a black box, pass it around, then unwrap it and use it later (maybe in a message queue, a mailbox, a private implementation, or in certain kinds of IOC patterns), you can completely lose your type and recover it later. Typically there is a small performance hit as you use RTTI (<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Run-time_type_information" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Run-time_type_information</a>) or a discriminator (an enum maybe) to decide how to recover your type when you&#x27;re ready for it. See &lt;experimental&#x2F;any&gt; (<a href="http:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;experimental&#x2F;any" rel="nofollow">http:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;experimental&#x2F;any</a>) for an example of this.<p>2b. Type Erasure<p>This one is more involved but clever use of generic programming lets us automatically provide the adaptor boilerplate to get types to play nice with other code. Sean Parent does an excellent walkthrough of this technique in his GoingNative talk in 2013 (<a href="https:&#x2F;&#x2F;channel9.msdn.com&#x2F;Events&#x2F;GoingNative&#x2F;2013&#x2F;Inheritance-Is-The-Base-Class-of-Evil" rel="nofollow">https:&#x2F;&#x2F;channel9.msdn.com&#x2F;Events&#x2F;GoingNative&#x2F;2013&#x2F;Inheritanc...</a>). Point being that type erasure lets you have a different flavor of flexibility in a way not seen in Java-land.<p>2c. Unions<p>This is harder to do properly in practice, as evidenced by the difficulty in standardizing something like boost::variant, but from time to time it might be worth playing around with various types of unions. They&#x27;re a bit awkward, but you might try boost::variant if you like the tradeoffs it provides.<p>3. Value Semantics<p>Since objects are values in C++, you need to know the size of things much more often. This causes dependencies you might not think about in other languages, depending on how their standard libraries are designed.