Scrupulous Abstractions

Spare time experimentation with C++11

Setup for C++11: Option 1/n: using Visual Studio 2012 + ‘CTP’


In this post I’ll use Visual Studio to write a short C++11 program that runs a few calculations asynchronously, making use of for example the new so called std::future.

Microsoft is actively working on C++11 support and plan to ship compiler backend upgrades between the major releases of visual studio. At this writing, the last such upgrade is known as v120_CTP_Nov2012. Even with this upgrade a few major things are missing. Most notably for a beginner, the new curly-brace initialization syntax via initializer_list is still missing. Still, much is there and worth exploring. This is how you get going:

Installing the latest version of Visual Studio:

  1. Install Visual Studio 2012. The express version is free and it will do. If you work on windows 7, use the ‘Desktop’ version. When asked about what to install, you can deselect everything except C++.
  2. Install Visual Studio updates - You only need vsupdate. This will update your Visual Studio installation.
  3. For the latest and most complete C++11 support from Microsoft, install their CTP (Community Technical Preview).

Creating a first C++ project:

  1. Open VS Express for Desktops.
  2. From the tab to the left, select New Project
  3. Select Win32 Console Application from the path ‘Installed/Templates/Visual C++’. Before you click OK pic a Name for your new application, such as MyFirstCpp11Program. Then select Finish.

Enable the latest C++ features from the CTP we installed:

  1. On the menu bar, select PROJECT/MyFirstCpp11Program Properties…
  2. Then find Configuration Properties/ General
  3. Change Platform Toolset from v110 to the latest CTP. As of this writing it Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)
  4. if you don’t know what it is, disable Precompiled Headers under C/C++ precompiled headers.
  5. Enable line numbers. I find that very helpful. You just need to click the box under the menu TOOLS/Options/Text Editor/All Languages/Display

Your program will initially look something like this:

#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[]){
return 0;
}

To try out the new features in C++11, remove all of that code, and instead insert my example, found at the end of this post.

To compile and run the program, press the green arrow: ‘Local Windows Debugger’.

There are many places to learn more about using visual studio. If you never did that before, try setting break points where the debugger will pause during the execution. Break points are created by double clicking the gray area to the left of the code, left of the line numbers if you followed my advice above to enable them.

Good luck!

Example C++11 Program



// FirstCPP11_Example.cpp : Defines the // entry point for the console application. // // C++11 Example, Johan Lundberg 2012
// http://scrupulousabstractions.tumblr.com/ #include<mutex> #include<future> #include<vector> #include<iostream> #include<stdexcept> #include<chrono> #include<string> class WeatherServer { public: WeatherServer() : current_temp(0) {}; ~WeatherServer() {}; /* Method calculating forcast for day, for * simplicity given as a plain integer. */ int CalculateForecast(unsigned int i){ /* Lock the mutex, using RAII, for the * duration of this calculation (for show). * Removing this will allow multiple * forecast calculations to run at the same time. * Else they will wait for each other */ std::lock_guard<std::mutex> locker(members_mutex); /* Sleep for some time just to show what's going on */ std::chrono::milliseconds dura( 701 ); std::this_thread::sleep_for( dura ); std::cout << "CalculateForecast done for i=" <<i<<std::endl; // result is just the square of the input... return i*i ; } private: int current_temp; std::mutex members_mutex; }; void my_main(){ auto myWS=std::make_shared<WeatherServer>(); std::vector<std::future<int>> forecasts; int v[]={0,1,2,3,4,5,6,7} ; for(auto i: v ){ // add a background calculation to the list forecasts.push_back( std::async(std::launch::async, [i,myWS]() { return myWS->CalculateForecast(i); } )); } /* This line will not compile if you use visual studio older * than the CTP Nov 2012: Current versions of visual studio will * underline it with red lines, but the actual compiler * understands it. Alternative for older compilers: std::string ctpTestString("Hello \"World!\" "); */ std::string ctpTestString=R"( Hello new "World!" )" ; std::cout << ctpTestString<<std::endl; int rainOnMonday = forecasts[0].get() ; int rainOnFriday = forecasts[4].get() ; std::cout << "rain amount on Monday:" << rainOnMonday <<std::endl; std::cout << "rain amount on Friday:" << rainOnFriday <<std::endl; std::cout << "hit enter to exit. " <<std::flush ; std::cin.ignore(1); } int main(){ try{ my_main(); }catch(std::exception& e){ std::cout << "main got exception:" << e.what() <<std::endl; throw; }catch(...){ std::cout << "main got unknown exception." <<std::endl; throw; } }
comments powered by Disqus