Boost is a widely used portable C++ libraries. It works well with C++ standard library (STL) and usable across a broad spectrum of apps or component framework, including C++/Cx and WinRT. In this tutorial, I would like to give you introduction on how you can utilize boost in your WinRT component project.
Get Your Boost Ready with VS
By the time you've completed the following steps, you'll be at least somewhat comfortable with the contents of a Boost distribution and how to use it with Visual Studio 2012.
1. Get Boost
The easiest way to get a copy of Boost is to use an installer, provided by BoostPro Computing, as it can download and install precompiled library binaries for you. Unfortunately by this time, no available installer for v.1.50.0 and VS 2012. The next question you will have is how to compile Boost library binaries then? The good news is there is nothing to build as “most” of libraries are header-only, they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking. Only small portion of Boost libraries need to be built separately, like Boost.FileSystem, Boost.IOStreams, Boost.MPI, etc. You will need Phyton to compile Boost separately, but that is out of scope in this tutorial, as we will focus on header-only library.
First, please download Boost v1.50.0 from www.boost.org and unpack it to your selected Boost directory. Please learn the Boost directory structure as following:

2. Build Simple Program using Boost and VS 2012
- From Visual Studio's File menu, select New > Project…
- In the left-hand pane of the resulting New Project dialog, select Visual C++ > General > Empty Project.
- In the name field, enter “BoostExample”.
- In Configuration Properties > C/C++ > General > Additional Include Directories, input Boost root directory, in my case D:\CPP\Libraries\boost_1_50_0.
- Make sure in Configuration Properties > C/C++ > Precompiled Headers, to Not Using Precompiled Headers.

- Copy the snipped codes below and from the Build menu, build the Solution.
- Test the application by entering “1 2 3” as input in command prompt console. The result should be “3 6 9”.
1: #include <boost/lambda/lambda.hpp>
2: #include <iostream>
3: #include <iterator>
4: #include <algorithm>
5: using namespace std;
6:
7: int main()
8: {
9: using namespace boost::lambda;
10: typedef istream_iterator<int> in;
11:
12: for_each(in(cin), in(), cout << (_1 * 3) << " " );
13: }
Boost Lambda Library (BLL) that we currently use is a C++ template library, which implements a form of lambda abstractions for C++. In our example :
for_each(in(cin), in(), cout << (_1 * 3) << " " );
The expression cout << (_1 * 3) << ' ' defines a unary function object. The variable _1 is the parameter of this function, a placeholder for the actual argument. Within each iteration of for_each, the function is called with an element of a as the actual argument. This actual argument is substituted for the placeholder, and the “body” of the function is evaluated. You can learn more about BLL from Boost library documentation and samples, that will be fun as it very rich, from algorithm and data structure, concurrent programming, memory management (smart pointers, pool), etc.
Leverage Boost in C++/Cx
Once you are familiar with Boost, you can use it in your applications, including WinRT based Metro apps, more specifically C++/Cx. The way you use Boost is very straight forward because most of the Boost libraries are header-only, designed to be portable, work well with STL and it has low usage of APIs not part of Windows SDK for Metro style app. In this case we can have the entire power of Boost to build Metro style apps like XAML, Direct2D, Direct3D and components.
However, C++/Cx introduced some new data types, such as platform string, strong pointer and strong reference. In that case, type conversion will be one of the issue as Boost APIs usually require/return standard types. For example, most API's in Windows Runtime (where Platform::String is mostly used) are Wide char and like other strings Platform::String does not have built in functionality to convert between Wide and Narrow.
The good news is C++ provides a variety of implicit conversions between Windows Runtime types and standard C++ types. In case of string, we can use Data() and Length() methods in Platform::String to obtain wchar_t* and length of the string and conversion interchange can be done using widechar strings. And also we can initialize Platform::String from standard string like wchar_t*. The typical pattern is to use standard C++ types and libraries internally as usual, and convert to Windows Runtime types in the public interfaces. Here is the sample from MSDN:
1: using namespace Platform;
2: #include <string>
3: // initializing a String^
4: String^ str1 = "Test";// no need for L
5: String^ str2("Test");
6: String^ str3 = L"Test";
7: String^ str4(L"Test");
8: String^ str5 = ref new String(L"Test");
9: String^ str6(str1);
10: String^ str7 = str2;
11: wchar_t msg[] = L"Test";
12: String^ str8 = ref new String(msg);
13: std::wstring wstr1(L"Test");
14: String^ str9 = ref new String(wstr1.c_str());
15: String^ str10 = ref new String(wstr1.c_str(), wstr1.length());
16:
17: // accessing chars into String^
18: auto it = str1->Begin();
19: wchar_t ch = it[0];
20:
21: // String^ is null terminated (but can contain L'/0', just like wstring)
22: assert(it[str1->Length()] == L'/0');
23:
24: // String^ is immutable
25: it[1] = L'A';// yields compile error C3892
26:
27: // initializing a std::wstring from String^
28: std::wstring wstr2(str1->Data());
29: std::wstring wstr3(str1->Data(), str1->Length());
30:
31: // comparisons
32: if (str1 == str2) { /* ... */ }
33: if (str1->Equals(str2)) { /* ... */ }
34: if (str1 != str2) { /* ... */ }
35:
Let see a simple case where I used Boost string algorithm to operate on wstr1 and initialized Platform::String to fill the text in a textbox. Please note that conversion comes with some costs, please use it efficiently!.
1:
2: void StringBoost::MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
3: {
4: String^ s = "This is WinRT platform String type";
5: tbxToken->Text = s;
6: }
7:
8:
9: void StringBoost::MainPage::btnToken_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
10: {
11: using namespace boost;
12: std::wstring wstr1(L"HELLO WORD");
13: std::wstring wstr2=to_lower_copy(ireplace_first_copy(wstr1,"hello","halo"));
14: tbxToken->Text = ref new String(wstr2.c_str());
15: }
Boost is huge library and you can start to use it in your Metro apps using C++/Cx. I hope you like it like I do.
Hope this helps!
Cheers – RAM