This project has been moved to GitHub.

New location: https://github.com/mariusbancila/mfccollectionutilities

C++11 has provided support for range-based for loops. They allow iterating over the elements of a range without using an index.

However, if you try the following MFC code you get some errors because the compiler is looking for a begin() and end() function that provides access to the first and last element of the range:

1>error C3312: no callable ‘begin’ function found for type ‘CStringArray’
1>error C3312: no callable ‘end’ function found for type ‘CStringArray’

MFC does not define such functions for its containers.

Enter MFC Collection Utilities

Tom Kirby-Green and I have developed a small open-source library that enables the use of all MFC collection types in range-based for loops. The library is called MFC Collection Utilities and is available on codeplex.

The library consists of a single header, called mfciterators.h, that you include in your MFC projects.

Compiler and collections support

The library works in Visual Studio 2012 (the first version of the C++ compiler that supports range-based for loops) or a newer version.

The library enables all the MFC collections, both template and non-template, to be used in range-based for loops. This means arrays, lists and maps. For maps you get access to the content through a key-value pair that has two fields: key and value.

Supported template collections

Arrays Lists Maps
CArray CList CMap
CTypedPtrArray CTypedPtrList CTypedPtrMap

Supported non-template collections

Arrays Lists Maps
CObArray CObList CMapPtrToWord
CByteArray CPtrList CMapPtrToPtr
CDWordArray CStringList CMapStringToOb
CPtrArray CMapStringToPtr
CStringArray CMapStringToString
CWordArray CMapWordToOb
CUIntArray CMapWordToPtr

Examples

Download

Version 1.0 can be downloaded from codeplex from here.

For simpler installation you can use the available nuget package.

mfccollectionutilitiesnuget

Let us know if you encounter any issues.

, , , , , , , Hits for this post: 33396 .
Trackback

3 comments untill now

  1. Is
    CTypedPtrList ptrlist;
    should be
    CTypedPtrList ptrlist;
    ?

  2. Can you elaborate? I don’t see any difference in what you posted. Where is my wrong code?

  3. I mean maybe the CBar* in CTypedPtrList template should be CFoo*?
    The angle brackets are eaten šŸ™‚

Add your comment now