Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / ATL

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

0.00/5 (No votes)
17 Apr 2012CPOL1 min read 7.4K  
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)