<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>jaredpar's WebLog : Templates</title><link>http://blogs.msdn.com/jaredpar/archive/tags/Templates/default.aspx</link><description>Tags: Templates</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Thread Local Storage template</title><link>http://blogs.msdn.com/jaredpar/archive/2008/04/21/thread-local-storage-template.aspx</link><pubDate>Mon, 21 Apr 2008 20:52:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8415085</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8415085.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8415085</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8415085</wfw:comment><description>&lt;p&gt;Thread local storage is another method of synchronization between threads.&amp;nbsp; It is different that most synchronization cases because instead of sharing state between threads it enables developers to have independent, thread specific pieces of data which have a similar or common purpose.&amp;nbsp; &lt;/p&gt; &lt;p&gt;The uses of thread local storage (TLS) vary greatly but is a very powerful and lightweight method for storing data.&amp;nbsp; TLS can easily be envisioned as a giant void* array for every thread.&amp;nbsp; The entry point, TlsAlloc, provides an index into this array and allows the storage of arbitrary data [1].&lt;/p&gt; &lt;p&gt;TLS is particularly useful for storing state information.&amp;nbsp; For example, one of my components lives in a highly multi-threaded environment.&amp;nbsp; Each thread serves essentially the same purpose and has the same states and state transition semantics.&amp;nbsp; Like any good paranoid programmer I wanted to add contracts to check my state transitions and semantics.&amp;nbsp; &lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Contract.VerifyState(ExpectedState, ???CurrentState)&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The question is where to store the state information for a thread?&amp;nbsp; A global state variable won't suffice because there are N threads.&amp;nbsp; A global array of state information also has it's share of problems: synchronization, determining an index, lifetime. &lt;/p&gt; &lt;p&gt;TLS is ideally suited to this scenario.&amp;nbsp; Each thread has an independent but similar concept of state.&amp;nbsp; In my initialization code I allocate an TLS index and now I have a place to store my state.&amp;nbsp; &lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Contract.VerifyState(ExpectedState, *TlsGetValue(g_stateTlsIndex)&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The next question is how to manage the lifetime?&amp;nbsp; TLS provides a void* and the caller must manage the lifetime of the allocated memory.&amp;nbsp;&amp;nbsp; Since this is thread specific the ideal place is to manage the memory in the thread startup proc.&amp;nbsp; However I don't own the creation of the thread, my component is called on a number of threads so this won't work.&amp;nbsp; &lt;/p&gt; &lt;p&gt;The solution is to use the stack.&amp;nbsp; The initial return for TlsGetValue is NULL.&amp;nbsp; If this situation is detected then the current stack frame is set to own the memory for the slot.&amp;nbsp; Further accesses to the value do not own the memory and simply access it.&amp;nbsp; The semantics are straight forward but annoying to constantly rewrite, so naturally write a template :)&lt;/p&gt;&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;typename&lt;/span&gt; T&amp;gt;
    &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; TlsValue
    {
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt;:
        TlsValue(DWORD index, &lt;span style="color: rgb(0,0,255)"&gt;const&lt;/span&gt; T&amp;amp; defaultValue=T()) :
            m_pValue(NULL),
            m_index(index),
            m_owns(&lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;)
        {
            m_pValue = &lt;span style="color: rgb(0,0,255)"&gt;reinterpret_cast&lt;/span&gt;&amp;lt;T*&amp;gt;(::TlsGetValue(m_index));
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; ( !m_pValue )
            {
                m_pValue = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; T(defaultValue);
                m_owns = &lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;;
                ::TlsSetValue(m_index, m_pValue);
            }
        }
        ~TlsValue()
        {
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; ( m_owns )
            {
                ::TlsSetValue(m_index, NULL);
                &lt;span style="color: rgb(0,0,255)"&gt;delete&lt;/span&gt; m_pValue;
            }
        }

        T* Value() &lt;span style="color: rgb(0,0,255)"&gt;const
&lt;/span&gt;        {
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; m_pValue;
        }

    &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt;:
        &lt;span style="color: rgb(0,128,0)"&gt;// Do not auto generate
&lt;/span&gt;        TlsValue();
        TlsValue(&lt;span style="color: rgb(0,0,255)"&gt;const&lt;/span&gt; TlsValue&amp;lt;T&amp;gt;&amp;amp;);
        TlsValue&amp;amp; &lt;span style="color: rgb(0,0,255)"&gt;operator&lt;/span&gt;=(&lt;span style="color: rgb(0,0,255)"&gt;const&lt;/span&gt; TlsValue&amp;lt;T&amp;gt;&amp;amp;);

        T* m_pValue;
        DWORD m_index;
        &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; m_owns;
    };
&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In addition to this blog post, I added a working sample to &lt;a title="http://code.msdn.microsoft.com/TlsValue" href="http://code.msdn.microsoft.com/TlsValue"&gt;http://code.msdn.microsoft.com/TlsValue&lt;/a&gt;.&amp;nbsp; This is my first attempt at posting a sample on &lt;a href="http://code.msdn.com"&gt;http://code.msdn.com&lt;/a&gt; so please provide any and all feedback on the data.&lt;/p&gt;
&lt;p&gt;[1] This is similar to data marked with the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.threadstaticattribute(VS.71).aspx"&gt;ThreadStatic attribute&lt;/a&gt; in managed code without all of the slot messiness and with the added benefit of strong typing.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8415085" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Templates/default.aspx">Templates</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Threading/default.aspx">Threading</category></item><item><title>Gotcha: CComPtrBase&lt;T&gt; assignment</title><link>http://blogs.msdn.com/jaredpar/archive/2008/04/08/gotcha-ccomptrbase-t-assignment.aspx</link><pubDate>Tue, 08 Apr 2008 18:44:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8352116</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8352116.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8352116</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8352116</wfw:comment><description>&lt;p&gt;Today what started out as a crash due to a pure virtual call turned into finding a gotcha in CComPtrBase&amp;lt;T&amp;gt;.&amp;nbsp; Essentially the code in question boiled down to the following.&amp;nbsp; Can you spot the problem?&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; GetAStudent(CComPtrBase&amp;lt;T&amp;gt; &amp;amp;spStudent)
{
    CComPtr&amp;lt;Student&amp;gt; spLocal;
    &lt;span style="color: rgb(0,128,0)"&gt;// Do some operation to get a student
&lt;/span&gt;    spLocal = spStudent;
}&lt;/pre&gt;
&lt;p&gt;The problem isn't apparent until you look at the definition for CComPtrBase&amp;lt;T&amp;gt;::operater =.&amp;nbsp; See the problem?&amp;nbsp; Basically CComPtrBase&amp;lt;T&amp;gt;::operator= isn't explicitly defined.&amp;nbsp; This means that C++ will automatically implement &lt;a href="http://msdn2.microsoft.com/en-us/library/x0c54csc(VS.71).aspx"&gt;memberwise assignment.&lt;/a&gt;&amp;nbsp; The RHS of the operator= will be a "const CComPtrBase&amp;lt;T&amp;gt;&amp;amp;".&amp;nbsp; &lt;/p&gt;
&lt;p&gt;CComPtr&amp;lt;T&amp;gt; derives from CComPtrBase&amp;lt;T&amp;gt; therefore it satisfies this and a memberwise assignment will occur.&amp;nbsp; We now have two smart pointers on the same value.&amp;nbsp; However the second smart pointer, CComPtrBase&amp;lt;T&amp;gt;, did not perform an AddRef.&amp;nbsp; So when both objects are destroyed there will be an extra Release and hopefully a crash.&lt;/p&gt;
&lt;p&gt;The fix? &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use CComPtr&amp;lt;T&amp;gt; or CComPtrEx&amp;lt;T&amp;gt; instead of CComPtrBase&amp;lt;T&amp;gt;&lt;/li&gt;
&lt;li&gt;Less Optimal: call AddRef() on spLocal.&amp;nbsp;&amp;nbsp; &lt;/li&gt;&lt;/ol&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8352116" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Templates/default.aspx">Templates</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Gotcha/default.aspx">Gotcha</category></item><item><title>Reference values in C++</title><link>http://blogs.msdn.com/jaredpar/archive/2008/04/03/reference-values-in-c.aspx</link><pubDate>Thu, 03 Apr 2008 17:24:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8345947</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8345947.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8345947</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8345947</wfw:comment><description>&lt;p&gt;Reference values are a powerful feature of C++ but I find they have one significant detractor.&amp;nbsp; A developer can not look at an API call and determine if a parameter is being passed by reference or value (VB has the same problem).&amp;nbsp; &lt;/p&gt; &lt;p&gt;IMHO this is one item that C# got 100% correct.&amp;nbsp; In C# developers must say a value is out/ref or a compile error results.&amp;nbsp; Forcing both the API declaration and usage to specify the reference semantics makes code much more understandable.&amp;nbsp; When you look at an API call there is absolutely no question about the byref/byval/out semantics of a parameter.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Internally I've met people who are hesitant to use reference parameters in C++ because of the ambiguity.&amp;nbsp; Not making it declarative in both places meant unexpected behavior could occur in a number of scenarios.&amp;nbsp; I agree with this statement.&amp;nbsp; &lt;/p&gt; &lt;p&gt;But hey we're talking about C++ here.&amp;nbsp; Any C++ problem can be fixed with some macros and a template right?&amp;nbsp; I thought about this over the weekend and came up with a quick sample.&amp;nbsp; Note, I haven't extensively tested this sample yet so there may be bugs.&amp;nbsp; However it gets the base cases right.&lt;/p&gt; &lt;p&gt;The goal of this API is to allow API authors to force developers to be explicit about their ByRef semantics.&amp;nbsp; It will prevent developers from silently passing a value by ref and hence getting unexpected behavior.&amp;nbsp; Failure to do so will result in a compile time error.&amp;nbsp; Also there is a minor bit of indirection overhead for debug mode but in retail this will compile out to normal code.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;#ifdef&lt;/span&gt; DEBUG

&lt;span style="color: rgb(0,0,255)"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; ByRefType
{
&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt;:
    &lt;span style="color: rgb(0,0,255)"&gt;explicit&lt;/span&gt; ByRefType(T&amp;amp; arg) : m_ref(arg)
    {
    }

    &lt;span style="color: rgb(0,0,255)"&gt;operator&lt;/span&gt; T&amp;amp;() &lt;span style="color: rgb(0,0,255)"&gt;const&lt;/span&gt; 
    {
        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; m_ref;
    }

    ByRefType&amp;amp; &lt;span style="color: rgb(0,0,255)"&gt;operator&lt;/span&gt;=(&lt;span style="color: rgb(0,0,255)"&gt;const&lt;/span&gt; T&amp;amp; value)
    {
        m_ref = value;
        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; *&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;;
    }

&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt;:
    ByRefType();

    &lt;span style="color: rgb(0,0,255)"&gt;mutable&lt;/span&gt; T&amp;amp; m_ref;
};

&lt;span style="color: rgb(0,0,255)"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;typename&lt;/span&gt; T&amp;gt;
ByRefType&amp;lt;T&amp;gt; MakeByRefType(T&amp;amp; expr)
{
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; ByRefType&amp;lt;T&amp;gt;(expr);
}

&lt;span style="color: rgb(0,0,255)"&gt;#define&lt;/span&gt; ByRef(expr) MakeByRefType(expr)
&lt;span style="color: rgb(0,0,255)"&gt;#define&lt;/span&gt; ByRefParam(type) ByRefType&amp;lt;type&amp;gt; 

&lt;span style="color: rgb(0,0,255)"&gt;#else

&lt;/span&gt;&lt;span style="color: rgb(128,128,128)"&gt;#define ByRef(expr) expr
#define ByRefParam(type) type&amp;amp;

&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;All well and good.&amp;nbsp; Now we can attribute byref paramaters with ByRefParam() and force callers to tag it as a ByRef argument.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; SimpleByRef(ByRefParam(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;) i, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; newValue)
{
    i = newValue;
}

&lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Test()
{
    &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i1;
    SimpleByRef(ByRef(i1), 5);
    SimpleByRef(i1, 6);     &lt;span style="color: rgb(0,128,0)"&gt;// Compiler Error!!!
&lt;/span&gt;}&lt;/pre&gt;As said before, this is an initial implementation and I expect updates as I use this in code and find bugs.&amp;nbsp; Please post back with any you find.&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8345947" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Templates/default.aspx">Templates</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/API+Design/default.aspx">API Design</category></item><item><title>Multiple paths to IUnknown</title><link>http://blogs.msdn.com/jaredpar/archive/2008/02/22/multiple-paths-to-iunknown.aspx</link><pubDate>Fri, 22 Feb 2008 09:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7845303</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/7845303.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=7845303</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=7845303</wfw:comment><description>&lt;P&gt;ATL has a lot of great tools for COM programming and &lt;A href="http://msdn2.microsoft.com/en-us/library/ezzw7k98(VS.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ezzw7k98(VS.80).aspx"&gt;CComPtr&lt;/A&gt; is a good example.&amp;nbsp; It's a smart pointer class which manages the reference count of an underlying COM object. &lt;/P&gt;
&lt;P&gt;One of it's limitations though is it will only work properly when the inheritance chain for a class has only one path to IUnknown.&amp;nbsp; If it has more than one path, the following error will be issued when you attempt to assign a value of type T to the CComPtr.&lt;/P&gt;
&lt;P&gt;error C2594: 'argument' : ambiguous conversions from 'SomeClass *' to 'IUnknown *'&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The reason behind this is the operator= for CComPtr uses &lt;A href="http://msdn2.microsoft.com/en-us/library/40d27a83(vs.71).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/40d27a83(vs.71).aspx"&gt;AtlComPtrAssign&lt;/A&gt; to change the references.&amp;nbsp; The right hand side of the assignment is passed to this function as IUnknown.&amp;nbsp; Since there are multiple paths to IUnknown the C++ compiler cannot implicitly perform the cast and issues the above error.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;I most frequently encounter this error in larger code bases with older classes.&amp;nbsp; New functionality is needed so I add a new interface and end up with a lot of C2594 errors.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;To work around this I defined a new CComPtr class named CComPtrEx which inherits from CComPtr base.&amp;nbsp; It defines the same operators as CComPtr but uses a Copy Constructor and Swap to perform the = which gets around the multiple paths to IUnknown.&amp;nbsp; The rest of the functions are identical to CComPtr.&amp;nbsp; &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;template&lt;/SPAN&gt; &amp;lt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt; T&amp;gt;
&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt; CComPtrEx : &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; CComPtrBase&amp;lt;T&amp;gt;
{
&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;:
    CComPtrEx() &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt;()
    {
    }
    CComPtrEx(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt; nNull) &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt;() :
        CComPtrBase&amp;lt;T&amp;gt;(nNull)
    {
    }
    CComPtrEx(T* lp) &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt;() :
        CComPtrBase&amp;lt;T&amp;gt;(lp)
    {
    }
    CComPtrEx(_In_ &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;const&lt;/SPAN&gt; CComPtrEx&amp;lt;T&amp;gt;&amp;amp; lp) &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt;() :
        CComPtrBase&amp;lt;T&amp;gt;(lp.p)
    {
    }

    T* &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;operator&lt;/SPAN&gt;=(_In_opt_ T* lp) &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt;()
    {
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;(*&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;!=lp)
        {
            CComPtrEx&amp;lt;T&amp;gt; sp(lp);
            Swap(&amp;amp;p, &amp;amp;sp.p);
        }
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; *&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;;
    }

    T* &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;operator&lt;/SPAN&gt;=(_In_ &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;const&lt;/SPAN&gt; CComPtrEx&amp;lt;T&amp;gt;&amp;amp; lp) &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt;()
    {
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;(*&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;!=lp)
        {
            CComPtrEx&amp;lt;T&amp;gt; sp(lp);
            Swap(&amp;amp;p, &amp;amp;sp.p);
        }
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; *&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;;
    }
};&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7845303" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Templates/default.aspx">Templates</category></item><item><title>C++ Placement New: Tracking down a crash</title><link>http://blogs.msdn.com/jaredpar/archive/2007/10/18/c-placement-new-tracking-down-a-crash.aspx</link><pubDate>Thu, 18 Oct 2007 20:05:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5510624</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/5510624.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=5510624</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=5510624</wfw:comment><description>&lt;p&gt;See my previous two posts on an introduction to placement new if you are unfamiliar with the subject.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a title="http://blogs.msdn.com/jaredpar/archive/2007/10/16/c-new-operator-and-placement-new.aspx" href="http://blogs.msdn.com/jaredpar/archive/2007/10/16/c-new-operator-and-placement-new.aspx"&gt;http://blogs.msdn.com/jaredpar/archive/2007/10/16/c-new-operator-and-placement-new.aspx&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a title="http://blogs.msdn.com/jaredpar/archive/2007/10/17/c-placement-new-and-allocators.aspx" href="http://blogs.msdn.com/jaredpar/archive/2007/10/17/c-placement-new-and-allocators.aspx"&gt;http://blogs.msdn.com/jaredpar/archive/2007/10/17/c-placement-new-and-allocators.aspx&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Recently I did a bit of work on the heap management story for our code base.&amp;nbsp; Mainly it was a change to unify the different ways we accessed our internal heap.&amp;nbsp; In the process I unified our operator new story.&amp;nbsp; Part if this involved unifying our placement new allocation overloads.&amp;nbsp; We have several allocators in our code base and I noticed we had several overloads which essentially did the same operation&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;inline
void&lt;/span&gt; * &lt;span style="color: rgb(0,0,255)"&gt;_cdecl&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;operator&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt;(size_t cbSize, MyCustomAllocator &amp;amp;allocator)
{
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; allocator.Alloc(cbSize);
}

&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;inline
void&lt;/span&gt; * &lt;span style="color: rgb(0,0,255)"&gt;_cdecl&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;operator&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt;(size_t cbSize, MyCustomAllocator *allocator)
{
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; allocator-&amp;gt;Alloc(cbSize);
}
&lt;/pre&gt;
&lt;p&gt;This seemed a bit redundant to me so I deleted the one which contained a pointer overload.&amp;nbsp; A while later I tested my changes and our application almost immediately crashed.&amp;nbsp; It only took a few minutes to discover the problem.&amp;nbsp; We had a lot of operations that followed this pattern.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;Student *p = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; (pAllocator) Student();  // pAllocator typed to MyCustomAllocator*&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;As I said in my previous posts, new is just another C++ function.&amp;nbsp; As such it participates in overload resolution.&amp;nbsp; This code now binds to the placement new operator and not the placement allocation overload which takes a reference.&amp;nbsp; At first I thought about using a regex search and replace to solve the issue.&amp;nbsp; However I decided this wasn't a complete solution.&amp;nbsp; There is no guarantee that my regex will catch every case and any case I miss won't crash until we actually hit the line of code.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The best case scenario here is to turn that line into a compile error.&amp;nbsp; That would guarantee that I fix every location that is a problem.&amp;nbsp; Since new participates in overload resolution you can solve this with a template.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;template&lt;/span&gt; &amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;typename&lt;/span&gt; T&amp;gt;
&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;inline
void&lt;/span&gt;* &lt;span style="color: rgb(0,0,255)"&gt;_cdecl&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;operator&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt;(size_t cbSize, T* allocator)
{
    allocator-&amp;gt;ThisWillForceACompileError();
}        &lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;This caused all places where a pointer type was passed to new which wasn't explicitly "void*" to turn into a compiler error.&amp;nbsp; This quickly outlined all of the places I needed to fix up and guaranteed that my fix didn't allow developers who were accustomed to using a pointer to the allocator to accidentally introduce a bug into the code base (myself included :)).&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5510624" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Templates/default.aspx">Templates</category></item></channel></rss>