Click here to Skip to main content
15,887,746 members
Articles / Desktop Programming / ATL

Unit Testing When the Success Case is Compilation Failure (with C++)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Apr 2012CPOL1 min read 7.3K   1  
The prescribed solution to this problem is to not place the COM types directly into the STL container but instead wrap them with the ATL CAdapt class.

There is an issue with the use of Window’s COM types being used with STL containers in versions of Visual Studio prior to 2010. The crux of the matter is that various COM types define the address-of operator and instead of returning the real address of the object, they return a logical address. If the STL container re-arranges its contents, this can lead to corruption as it can unwittingly manipulate an object at the incorrect address.

The prescribed solution to this problem is to not place the COM types directly into the STL container but instead wrap them with the ATL CAdapt class. When adopting this approach, there are two important aspects to consider:

  • Firstly, is there a way to prevent incorrect usage of the containers?
  • Secondly, assuming this is possible, is there a way to make sure it covers the various permutations of STL containers and COM types that are being used?

It turns out that the first issue is easily solved using template specialization. The hard part though is creating a set of unit tests that ensure specializations have been created for all permutations and that they are active. This is made even more difficult by the fact that a successful use of the specialization results in compilation failure.

This too can be solved by combing various template mechanisms and techniques: SFINAE & Template Meta-programming, etc. In the April issue of the ACCU Overload journal, I wrote an article that describes this problem along with the solution. Please follow the link and then jump to page 23 in the PDF.

The complete source code of the article is also available on GitHub: https://github.com/petebarber/TemplateArticleExamples.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United Kingdom United Kingdom
My day job is mostly working in C++ with a bit of C#. I write a fair amount of command line based tools and really wish they could have a GUI front-end to them hence why I spend my spare time working with WPF.

I started a blog few years back but didn't do a lot with it. I've started describing some of the interesting programming things I come across on it. Please take a look.

Comments and Discussions

 
-- There are no messages in this forum --