I this post I will talk about the deployment changes in VC++ 2010. When you deploy an application to another machine you have to install not only the application but all the libraries that it depends on. When you build with VC++, you have dependencies on CRT (C/C++ runtime) and possible on MFC and/or ATL.

Visual Studio 2005 introduced a new deployment model for Windows client applications based on isolated applications and side-by-side assemblies. Assemblies can be either shared (globally registered in the system, installed in the Global Assembly Cache – GAC folder in Windows – and available to all applications) or side-by-side (described with a manifest, distributed with the application and available only to that application).

In Visual C++ 2005, library assemblies (such as MFC, ATL, CRT) have been rebuilt as shared side-by-side assemblies and installed in the native assembly cache, WinSxS folder in Windows. That means they are not globally registered in the system, but are globally available to the applications that specify a dependency with a manifest file.

With VC++ 2005 or 2008 there are several options for deployment:

  • static linking: when you link your application statically against VC++ libraries (CRT, MFC or ATL) the application doesn’t have any dependencies so you don’t have to deploy any other VC++ DLLs to the target machine
  • shared side-by-side assemblies: the VC++ DLLs are deployed in the WinSxS folder; this can be done either with the Visual C++ Redistributable Merge Modules or the Visual C++ Redistributable Package; the application requires a manifest file that describes the dependent DLLs and their version
  • private assemblies: the VC++ DLLs are all installed in the same folder with the application; the application requires a manifest file

When you deploy an application built with Visual Studio 2005 or 2008 a manifest file that describes the dependencies, whether you deployed these VC++ DLLs in the local folder or they where installed in the WinSxS folder. If the manifest is missing you get an error. The next image shows the error received when running an MFC application (called Wordpad2008) build with VC++ 2008 on another machine without a manifest.

Though the purpose of this change was to simplify deployment, the result was probably the opposite. As a result Microsoft changed deployment requirements in Visual C++ 2010. You can now deploy applications without a Fusion or satellite manifest. All you need to do is copy the VC++ dependent DLLs to the application folder and run. The next image shows an MFC application (called Wordpad2010) built with VC++ 2010 running on another machine, without a satellite assembly. No error occurs any more when trying to start the application, because local deployment no longer require a satellite manifest.

With VC++ 2010 there are several options for deployment:

  • static linking: same as earlier
  • central deployment: the VC++ DLLs are deployed in the system32 folder; this is useful for updates, because Windows automatically identifies and updates the DLLs that are deployed here
  • local deployment: the application executable and its dependent DLLs are all installed in the same folder; no manifest file is required.

To find more information about deployment and manifest files I suggest these links:

, , Hits for this post: 17179 .

VC++ Feature Pack that came with Visual Studio 2008 SP1 introduced support for the Office Fluent Ribbon. However, developers had to create ribbons entirely from code, because there was no support in the resource editor for that. Visual Studio 2010 comes with a visual designer for the ribbon.

You can choose whether to use a ribbon or a classical menu and toolbar when you create an application.

By default, the created ribbon has one category (Home) and two panels with several commands.

The ribbon can be opened from the resource editor. There is a new category called Ribbon. By default the ribbon resource is called IDR_RIBBON. The description of the ribbon is kept in an XML file called ribbon.mfcribbon-ms, located in the res folder.

When the ribbon is opened, the toolbar displays controls that can be dragged and drop into the ribbon, including categories, context categories, panels, and a series of controls such as buttons, checkbox, edits, progress bar, slider, etc.

There is support for several styles, Office like and Windows 7. These different styles can be seen in the following image.

The designer provides support for quick testing of the ribbon. On the Ribbon Editor toolbar there is a button called Test Ribbon that opens window with the ribbon. You can quickly see how it will look in the application, however, the commands are not available; clicking on the ribbon commands does not have any effect.

You can add handlers for the ribbon commands just like you do for a menu or a toolbar. In Visual Studio 2010 this can be done with the class wizard.

You can read more about the ribbon designed in MSDN or the VC++ Team’s Blog.

, , Hits for this post: 17280 .

Some of the important changs in Visual Studio 2010 in regard to VC++ are represented by the support in the C++ compiler of some of the features already approved for the new C++ standard, so far called C++0x. In this post I will give a short overview on then.

static_assert

I already wrote a post about this feature. At that time I considered it rather a niche feature. However, this looks much powerful in conjunction with the type traits classes from TR1.

static_assert checks if an expression is true at compile time. If the expression if false a custom error message is displayed and the compilation fails. If the expression is true the declaration has no effect.

In the following example I create a comparison template function, that is used later to compare values.

template < typename T >
bool CompareNumbers(T v1, T v2)
{
   return v1 > v2;
}

int main()
{
   bool ret1 = CompareNumbers(1, 20);
   bool ret2 = CompareNumbers("b", "a");

   return 0;
}

I want this function to be used only for integral types (the reason doesn’t matter) and I’d like the compiler to issue an error when used with any other type. Adding a static_assert check will generate a compilation error for the second call to the function, when passing strings.

#include < type_traits >

template < typename T >
bool CompareNumbers(T v1, T v2)
{
   static_assert(std::tr1::is_integral< T >::value, "Type is not numeric");
   return v1 > v2;
}
1>d:\marius\vc++\cpp0x\cpp0x.cpp(62): error C2338: Type is not numeric
1>          d:\marius\vc++\trainnings\cpp0x\cpp0x.cpp(75) : see reference to function template instantiation 'bool CompareNumbers(T,T)' being compiled
1>          with
1>          [
1>              T=const char *
1>          ]

auto

If you are familiar with C#, this is the C++ equivalent of var. The keyword is used to deduce the type of a declared variable from its initialization expression. The initialization expression can be an assignment, direct initialization or operator new expression. It must be noted that the auto keyword is just a placeholder, not a type, and cannot be used with sizeof or typeid.

auto i = 13;        // i is int
auto s = "marius";  // s is std::string
auto p = new foo(); // p is foo*

vector< int > numbers;
generate_n(back_inserter(numbers), 10, rand);
for(auto it = numbers.begin(); it != numbers.end(); ++it)
{
   cout << *it << endl;
}

lambda expressions

I already wrote about lambdas, but I will give a short overview again. Again, if you are familiar with C# and .NET, this is the same concept as in .NET.

A lambda functions is a function object whose type is implementation dependent; its type name is only available to the compiler. The lambda expression is composed of several parts:

  • lambda_introducer: this is the part that tells the compiler a lambda function is following. Inside the angled brackets a capture-list can be provided; this is used for capturing variables from the scope in which the lambda is created.
  • lambda-parameter-declaration: used for specifying the parameters of the lambda function.
  • lambda-return-type-clause: used for indicating the type returned by the lambda function. This is optional, because most of the time the compiler can infer the type. There are cases when this is not possible and then the type must be specified. For the example above, the return type (-> bool) is not necessary.
  • compound-statement: this is the body of the lambda.
vector<int> numbers;
generate_n(back_inserter(numbers), 10, rand);

for_each(numbers.begin(), numbers.end(), [](int n) {cout << n << endl;});

Here [] is the lambda introducer, (int n) is the lambda parameter declaration, and {cout << n << endl;} is the lambda compound statement. There is no return type clause, because that is auto inferred by the compiler. There are cases when the compiler cannot deduce the return value and then it must be specified explicitly. A lambda expression is a syntactic shortcut for a functor. The code above is equivalent to:

class functor_lambda
{
public:
   void operator()(int n) const
   {
      cout << n << endl;
   }
};

vector<int> numbers;
generate_n(back_inserter(numbers), 10, rand);

for_each(numbers.begin(), numbers.end(), functor_lambda());

Lambdas can capture variables from their scope by value, reference or both in any combination. In the example above, there was no value captured. This is a stateless lambda. On the other hand, a lambda that captures variables is said to have a state.

rvalue references

Stephan T. Lavavej wrote the ultimate guide to rvalue references. There is nothing more that can be said that is not already there. I strongly suggest you read his article to familiarize with this concept.

rvalue references are used to hold a reference to a rvalue or lvalue expression, and are introduced with &&. They enable the implementation of move semantics and perfect forwarding.

Move semantics enable transferring resources from one temporary object to another. This is possible because temporary objects (i.e. rvalues) are not referred anywhere else outside the expression in which they live. To implement move semantics you have to provide a move constructor and optionally a move assignment operator. The Standard Template Library was changed to take advantage of this feature. A classic example for the move semantics is represented by operation with sequences like vector or list. A vector allocates memory for a given number of objects. You can add elements to it and no re-allocation is done until the full capacity is reached. But when that happens, the vector has to reallocate memory. In this case it allocates a new larger chunk, copies all the existing content, and then releases the pervious memory. When an insertion operation needs to copy one element several things happen: a new element is created, its copy constructor is called, and then the old element is destroyed. With moves semantics, the allocation of a new element and its copy is no longer necessary, the existing element can be directly moved.

A second scenario where rvalue references are helpful is the perfect forwarding. The forwarding problem occurs when a generic function takes references as parameters and then needs to forward these parameters to another function. If a generic function takes a parameter of type const T& and needs to call a function that takes T&, it can’t do that. So you need an overloaded generic function. What rvalue references enable is having one single generic function that takes arbitrary arguments and then forwards them to another function.

decltype operator

This is used to yield the type of an expression. Its primary purpose is for generic programming, in conjunction with auto, for return types of generic functions where the type depends on the arguments of the function. Here are several examples:

double d = 42.0;     // decltype(i) yields double
const int&& f();     // decltype(f()) yields const int&&
struct foo {int i;}; // decltype(f.i) yields int (f being an object of type foo)

It can be used together with auto to declare late specified return type, with the alternative function declaration syntax, which is (terms in squared brackets indicate optional parts)

auto function_name([parameters]) [const] [volatile] -> decltype(expression) [throw] {function_body};

In general, the expression use with decltype here should match the expression used in the return statement.

struct Liters
{
   double value;
   explicit Liters(double val):value(val){}
};

struct Gallons
{
   double value;
   explicit Gallons(double val):value(val){}
};

ostream& operator<<(ostream& os, const Liters& l)
{
   os << l.value << "l";
   return os;
}

ostream& operator<<(ostream& os, const Gallons& g)
{
   os << g.value << "gal";
   return os;
}

Liters operator+(const Liters& l1, const Liters& l2)
{
   return Liters(l1.value + l2.value);
}

Gallons operator+(const Gallons& g1, const Gallons& g2)
{
   return Gallons(g1.value + g2.value);
}

Liters operator+(const Liters& l, const Gallons& g)
{
   return Liters(l.value + g.value*3.785);
}

Gallons operator+(const Gallons& g, const Liters& l)
{
   return Gallons(g.value + l.value*0.264);
}

template <typename T1, typename T2>
auto Plus(T1&& v1, T2&& v2) -> decltype(forward< T1 >(v1) + forward< T2 >(v2))
{
   return forward< T1 >(v1) + forward< T2 >(v2);
}

int main()
{
   cout << Plus(l1, l2) << endl;
   cout << Plus(g1, g2) << endl;
   cout << Plus(l1, g1) << endl;
   cout << Plus(g2, l2) << endl;

   return 0;
}

The result of the execution is:

15l
30gal
42.85l
22.64gal

When function Plus is called with arguments of the same type, the result is that type. But when the arguments differ, the resulting type is also different. In this example, when the first argument is Liters and second is Gallons, the result type must be Liters and the opposite. It is possible to do this without decltype, but the solution requires explicit specification of the resulting type.

template <typename T, typename T1, typename T2>
T Plus(T1&& v1, T2&& v2)
{
   return forward< T1 >(v1) + forward< T2 >(v2);
}

int main()
{
   cout << Plus<Liters>(l1, l2) << endl;
   cout << Plus<Gallons>(g1, g2) << endl;
   cout << Plus<Liters>(l1, g1) << endl;
   cout << Plus<Gallons>(g2, l2) << endl;

   return 0;
}
, , , , , Hits for this post: 19583 .

In my previous post I talked about the new build system for VC++ from Visual Studio 2010, which is MSBuild and the support for multi-targetting. In this post I will talk about changes to IntelliSense and browsing.

If you go back to the example I was providing in the first post, with the two identical projects created with Visual Studio 2008 and Visual Studio 2010, a second important thing to notice in the comparison of the two solution is that the infamous .NCB file is no longer present in Visual Studio 2010 solution. Instead there is a new file with extension .SDF. This is not just a renaming of the extension, the entire Intellisense for Visual C++ was redesigned in Visual Studio 2010. This is a SQL Server Database file, possible to be opened even in Visual Studio (if one wants to check its content).

In the previous versions of Visual C++, each time you modified a header, the entire solution was reparsed, in which time it was very hard to use the environment. Moreover, the IntelliSense database file (the .NCB file) never seem to shrink, only increased in size, and it could get corrupted from time to time. In the new version, files are parsed on the background, and the IDE does not read all the files, only the current translation unit (which is a source file and all the headers it includes directly and indirectly). As a result, the operation is much swifter and less error prone.

There is also a new disk folder called iPCH in the new solution. This is the storing location for IntelliSense support files and browsing database files (SDF).

#include auto completion

Part of the new IntelliSense and Browsing experience, the #include keyword supports auto-completion for the header files. That means that after typing #include, the IDE displays a list of available headers, filter by their name as you type. The following image shows this.

Call Hierarchy

This feature enables navigation through the code, showing the calls to and from a selected method, constructor or property. When selecting a call in the hierarchy window it shows the code where the call is made.

Red Squiggles

This is a feature that enables highlighting syntactic and semantic errors with a red squiggle line. Hovering the mouse over the line will show a balloon with the error message. The same error is also listed in the Error List window.

Find All References

In the previous versions, this features displayed only the compiler verified results for a search. If you searched for a function M member of a class C it only returned the references where function M was used in the context of C. The new version allows two types of search: one that is focus on speed, and returns all the matches for a symbol regardless the context (but it’s a narrowed search than the one performed with Find in Files), and one that is focused on accuracy and returns only the compiler verified results (i.e. the ones that match the search context).

Class Wizard

Yet another important change is the famous and acclaimed class wizard from VC6, that was dropped in Visual Studio 2002, and was now brought back in Visual Studio 2010.

If you are (or were) familiar with VC6 you know what the Class Wizard is. In Visual Studio 2010 it features basically the same functionality, except that it is improved with search functionality. You can search for command, messages, virtual functions, members or methods. This is great because might not know the exact name of a message or a function, but searching allows you to quickly get it with only typing part of the name. For those not familiar with VC6 this is a single point to add or remove commands, message handlers, virtual functions, member variables and methods. This was a favorite feature in VC6 for a lot of people and there was a constant pressure on Microsoft to bring it back, so here it is.

All these features are detailed in MSDN and on the VC++ Team blog. I suggest several additional readings:

, , , , Hits for this post: 20974 .

The new version of Visual Studio, called Visual Studio 2010 comes with a series of changes for Visual C++. This includes a new build system, new project system, multi-targeting, new IntelliSense, support in MFC for new controls, new additions to the C++ compiler (which were already approved for C++0x), new deployment model, and others. In this post I will talk about the new build system and multi-targeting.

In order to show the changes I will create two simple projects, one in Visual Studio 2008, called Wordpad 2008, and one in Visual Studio 2010, called Wordpad 2010. These would be simple MFC single document applications. The image bellow shows the two solutions opened in Solution Explorer.

As you can see both versions contain the same solutions file (only the suffix in the name differs). The next image shows the files on disk, in comparison for the two solutions.

MS-Build System

The first thing to notice (though it might not be the obvious) is that the project file extension was modified. In Visual Studio 2008 it is called .vcproj, but in Visual Studio 2010 is called .vcxproj. Not only the extension changed, but also the content of the file. This is because in Visual Studio 2010, Visual C++ build system was changed from VCBuild to MSBuild. This build engine was already used for the languages targeting the .NET framework.

MSBuild uses XML project files, and the most important elements of a project are:

  • Items: units of input into the build system, grouped into item collections, which can be used as parameters to the tasks, using the syntax @(ItemCollectionName). Examples of items from the Wordpad2010 project:
      < ItemGroup >
        < ClInclude Include="MainFrm.h" / >
        < ClInclude Include="Resource.h" / >
        < ClInclude Include="stdafx.h" / >
        < ClInclude Include="targetver.h" / >
        < ClInclude Include="Wordpad2010.h" / >
        < ClInclude Include="Wordpad2010Doc.h" / >
        < ClInclude Include="Wordpad2010View.h" / >
      < /ItemGroup >
    
  • Properties: pairs of key/value used to configure the builds. The value of a property can be changed after it was defined. They can be referred in the project file using the syntax $(PropertyName). Examples of properties from the Wordpad2010 project.
      < PropertyGroup Label="Globals" >
        < ProjectGuid >{1E7DC2AA-8CAC-44A8-98F6-DE69249AD30C}< /ProjectGuid >
        < RootNamespace >Wordpad2010< /RootNamespace >
        < Keyword >MFCProj< /Keyword >
      < /PropertyGroup >
    
  • Tasks: reusable units of executable code used to perform builds. Example of tasks can be compiling input files, linking, running external tools. Tasks can be reused in different projects.
  • Targets: represent groupings of tasks in a particular order and expose parts of the project file as entry points into the build system.

You can get a deeper overview on the MSBuild engine here.

Another thing to notice is the presence of a file called Wordpad2010.vcxproj.filters. This file defines the solution explorer tree with the files contained in the project. This used to be a part of the file project, but in Visual Studio 2010 it was moved into a separate file. The reason is to keep the project file only for the build, not for the organization of the project.

The user specific settings used to be stored in a file called ProjectName.vcproj.fullyqualifiedusername.user. Now there is a new file called ProjectName.vcxproj.user.

You can read more about these changes in MSDN.

Multi-targeting

Visual Studio 2008 came to support for multi-targeting of the .NET framework, not only for C# and VB.NET, but also for C++/CLI. In addition to that, Visual Studio 2010 comes with support for native multi-targeting.

The managed multi-targeting allows to target different versions of the .NET framework for mixed-mode applications. By default the target version is the latest, 4.0. This can only be changed manually in the project file. The support for changing this from the IDE was not included in this version. Actually it was dropped, because in Visual Studio 2008 this was possible.

  < PropertyGroup Label="Globals" >
    < ProjectGuid >{AB3D9231-F8B6-4EAD-A15B-C792977AB26E}< /ProjectGuid >
    < RootNamespace >MixedModeDemo< /RootNamespace >
    < TargetFrameworkVersion >v3.5< /TargetFrameworkVersion >
    < Keyword >MFCDLLProj< /Keyword >
  < /PropertyGroup >

The native multi-targeting allows to use different versions of the tools and libraries to build (native) C++ projects. Of course, you must have the targeted toolset installed on your machine, in order to do that. You can define different configurations that target different versions of the toolsets. The targeted toolset can be changed from project’s properties page, General, Platform Toolset. The following image shows the available options on a machine with Visual Studio 2008 SP1 and Visual Studio 2010 installed side by side.

It is possible to target the previous version, 2008, 2005, 2003 and 2002. In theory it’s possible to target even VC6, but there is no support from Microsoft for that.

I suggest to read more about native multi-targeting here, and about managed multi-targeting, for mixed-mode applications, here.

In a next post I will talk about the changes to IntelliSense and browsing experience.

, , Hits for this post: 17586 .

I think application development faces two challenges nowadays: 64-bit and multi-core/many-core hardware. Switching from 32 to 64 bit is just another step in the evolution of processors. There was a time when we switched from 8 to 16, and then from 16 to 32. There are problems that arise every time, but probably in 10-20 years we will have 128 bit platforms. On the other hand, multi-core/many-core is a different shift not only in development but also thinking. We either run one core or multiple core processors. My working stations has 8 cores (4 physical and 4 virtualized) and so does my laptop. Still, I don’t see an 8 times improvement of the applications running on these machines; yet I know 8 times I not what I should expect. But not even 4 times. For instance, the time for building from scratch the application I’ve working on with Visual Studio 2008 dropped from 20 minutes to 8 minutes; that’s a 2.5 improvement. I know that having N cores doesn’t mean that applications can run N times faster, because not everything can run in several threads, and then we have problems with resource access, synchronization and others. All these prevent applications run N times faster. But the problem is that we are not thinking in parallel. We are still used to program in a single thread; and when I say that I mean not only most developers are not used to do parallelization for boosting performance of some routines, but also many operations are run in the main (UI) thread, making that GUI freeze.

Therefore I would like to get some feedback from people working in applications development to get an idea about awareness, issues, solutions regarding concurrency. Please take several minutes to answer the questions in this survey.

I’d like to quote James Reinders, lead evangelist and Director of Marketing and Business Development for Intel Software Development Products, who said that:

I am still confident that software development in 2016 will not be kind to programmers who have not learned to “Think Parallel.”

The “not parallel” era we are now exiting will appear to be a very primitive time in the history of computers when people look back in a hundred years. The world works in parallel, and it is time for computer programs to do the same.

Doing one thing at a time is “so yesterday.”

The questionnaire bellow is also available here.

Thank you for answering the questionnaire.

, , , Hits for this post: 7440 .