Exercise 1: New Editor Features for Visual C++
In this exercise, you will discover the new C++ features of Visual Studio that will improve your productivity. You will work with a console application with boilerplate code already in place and insert simple code logic, mainly to showcase some of these new features:
Before starting this exercise, make sure you have the Visual Studio text editor configured properly. Select the Tools | Options item in the menu to open the configuration dialog. In the left pane tree, open the Text editor | C / C++ | Advanced leaf to enter the IntelliSense options and verify the following attribute values:
- Disable IntelliSense:
False
- Disable Member List Code Snippets:
False
- Disable Semantic Colorization:
False
- Member List Filter Mode:
Fuzzy
Task 1 – Project Compatibility
One of the pillars of this release is project compatibility from one version to the next. Now, you can open Visual Studio 2010 projects in Visual Studio 11 and continue working from Visual Studio 2010 without any problems since the project files are the same.
In this task, you will see how easy it is to migrate your solutions to Visual Studio 11 while maintaining compatibility with Visual Studio 2010. To learn about this feature, you will open a Visual Studio 2010 solution in Visual Studio 11, modify its contents and then reopen it in Visual Studio 2010.
Open Visual Studio 2010 if you have it installed in your environment; otherwise continue to step 3. Open the WhatsNewCpp-Ex1-Begin.sln solution located under the Source\Ex1-NewFeatures\Begin folder of this lab.
This solution was created using Visual Studio 2010; therefore, you can open it without any problem in that version of the IDE. If you want, you can also build and run the solution.
- Close Visual Studio 2010.
- Now open Visual Studio 11.
Select File | Open | Project/Solution. In the Open Project dialog, navigate to the Source\Ex1-NewFeatures\Begin folder of this lab and open WhatsNewCpp-Ex1-Begin.sln.
Notice that the solution was created using Visual Studio 2010, and Visual Studio 11 opened this solution without requiring any conversion.
As project files don’t require upgrading, you won’t see the Project Updater Wizard from the previous editions. If you have Visual Studio 2010 SP1, you can still open projects in Visual Studio 2010 after you’ve opened and saved them with Visual Studio 11.
You can build your project using Visual Studio 2010 tools in Visual Studio 11 using the multi targeting feature (requires Visual Studio 2010 with Visual C++ be installed side-by-side with Visual Studio 11). If you have installed Visual Studio 2010 with Visual C++ you can build your project with the Visual Studio 2010 tools, or you can change the targeting toolset to Visual Studio 11. This means that while you adapt to the new compiler and your 3rd party vendors start providing you with binaries compatible with the Visual Studio 11 compiler, you can still leverage the new Visual Studio 11 IDE without disrupting your ship cycle.
The Solution Explorer shows you that this project was built using the Visual Studio 2010 tools.
Figure 1
This project was built using the Visual Studio 2010 tools
- In the Solution Explorer, right-click the WhatsNewCpp project and select Properties.
In the General section of the property pages, change the Platform Toolset to Visual Studio 11 (v110) and click OK. This will configure the solution to build your project using the Visual Studio 11 toolset.
The Platform Toolset enables the project to target a different version of the Visual C++ libraries and compiler. This enables you to take advantage of the IDE enhancements in Visual Studio 11 while you continue to use an older version of the Visual C++ libraries and compiler.
Figure 2
Changing the platform toolset
Since there is no project upgrade, you can continue loading the project/solution in Visual Studio 2010 as long as you do not change the Visual Studio 2010 tools version (v100) used to build the project.
The Solution Explorer no longer displays (Visual Studio 2010) after the project name.
Figure 3
The project now uses the Visual Studio 11 toolset
Press CTRL+SHIFT+B to build the solution. The project now builds using the Visual Studio 11 tools (v110).
If you do not have Visual Studio 2010 with Visual C++ installed on your system, you may notice you cannot build the project with the original toolset (v100). Remember to switch to the appropriate toolset depending on which version of Visual Studio your project will be opened with.
You can also update the target toolset to use v110 (and .NET Framework 4.5) for all the Visual C++ projects in your solution at once. In the Solution Explorer, right-click on the solution node and select Upgrade VC++ projects.
Figure 4
Upgrading all the Visual C++ projects to the new toolset
Click Yes to confirm the operation. The Output Window displays the results of upgrading the solution.
Figure 5
Confirm upgrade operation
Figure 6
The solution has been upgraded
Task 2 – IntelliSense
In this task, you will discover some of the new enhancements included in C++ IntelliSense by completing the solution you have been working on.
- In the Solution Explorer, expand the Source Files folder and open Greetings.cpp. Locate the constructor of the class, Greetings::Greetings, which is now empty.
Implement the constructor by assigning the local variable greetingsType to the values passed in the arguments. To do this, start by typing this-> to access the class members inside the Greetings constructor.
Notice that Visual Studio automatically shows the member drop-down list while you are typing without having to invoke it explicitly.
Figure 7
Members list is shown automatically
You have probably already noticed that the code colorization has been improved in this version. In addition to the keywords, strings and comments, now other tokens like types, enumerations and macros are colorized; the parameters are in italics and so on. You will learn more about this later on.
Continue typing the greetingsType member name and pay attention to the members list displayed.
Notice that the list is being filtered while you are typing in order to show only the relevant members. In addition, you may notice a tooltip to the right of the list, which displays additional information on the selected member, such as the definition, its location and a brief description.
Figure 8
List filtered to show only relevant members
Complete the line by assigning the value from the greetingsType argument.
Greetings::Greetings(GreetingsType greetingsType, std::string userName)
FakePre-93434628453542fa8010eeafecfcb53f-c78cca5e0b3c4b90a8e284a25a2a87d0 this->greetingsType = greetingsType;FakePre-79374a82860149a5817954fc63b0dec7-8cb04a5e5da848cd907816134c5e6806
Continue to assign the value for the userName member. Type this->un and press TAB.
Notice that un is not a prefix or even a substring of the members in the list. The filtering uses fuzzy logic to quickly find the relevant members.
Figure 9
Camel casing based filtering
Complete the line by assigning the value of the userName member as shown below.
Greetings::Greetings(GreetingsType greetingsType, std::string userName)
FakePre-8bc32966a56346b7b2131ce309cc6090-96104fa4e0654174992d90df1c1ce9d1FakePre-2c905fed466b46da9153b2b96019dc20-f380cb36fc934c0ea6ab15b2dbfe252f this->userName = userName;FakePre-09a8bde3433a419cb2ba36ca5aaaec01-531ba04adaa948be884d0bed1f84a0a1
- Open WhatsNewCpp.cpp.
In the main function, create an instance of the Greetings class. To do this, type “Greetings greetings(”. Once you have opened the bracket, notice the tooltip showing the constructor reference. You can also switch to any of the available overloads by pressing the up and down arrow keys.
Figure 10
Displaying help for the Greetings constructor
Complete the constructor by using any GreetingsType item and your name.
Greetings greetings(GreetingsType::WELCOME, "User");
Invoke the Show function from the greetings object. While listing the class members, notice the tooltip showing help for each of the members of the class.
Figure 11
Displaying help for the class members
Task 3 – Code Snippets
Code snippets help you quickly type boilerplate code with minimal keystrokes. This feature was not available for C++ developers in previous versions of Visual Studio. Now, as you start typing, the IntelliSense member list also includes code snippets, which can be selected by pressing tab.
In this task, you will learn how to use Visual C++ code snippets while completing your application.
- Open Greetings.cpp.
Locate the Show function and place the cursor in the first line. Start typing switch, while paying attention to the IntelliSense list.
Notice that IntelliSense is displaying a code snippet to assist you in creating the switch structure.
Figure 12
Switch code snippet in the members list
Press TAB to insert the code snippet.
Notice that an empty switch structure has been inserted, and the cursor is now placed inside the switch expression.
Figure 13
Inserting the switch code snippet
Inside the switch expression, type greetingsType (or press TAB to autocomplete) and press ENTER.
Notice the switch structure is completed with the possible values for the greetingsType enum.
Figure 14
Completing the switch expression
In addition to the switch statement, there are other snippets for basic code constructs available to you – like if-else, for loop, etc. Each these snippets saves you from unnecessary typing and lets you focus more on your logic, adding up to significant productivity gains over time.
On each of the conditions (HELLO, HI, WELCOME), add a cout instruction that prints the message to the console.
void Greetings::Show()
FakePre-f15f7a743a374ecea2149db878ec8d3b-ecd6b47b14eb43a5b3d3c1faa55d1463 switch (greetingsType) { case HELLO: std::cout << "Hello"; break; case HI: std::cout << "Hi"; break; case WELCOME: std::cout << "Welcome"; break; default: break; }FakePre-0fb8138be6564db7b8e760e0a7426344-669d9238dcfb487294b1a786751297cb
- Below the greetingsType switch, use another cout instruction to print the username value as shown below.
void Greetings::Show()
FakePre-eecc30485e48484c98e97d8a84f32ab6-890a52a45b6b457fbd9800c8520c1d48FakePre-56b1c3f05cd441f09cfcee62c60f3c6e-66f9f9e80138424982fa890ad23ecde4FakePre-e46ef9ea500d4d8fa10e3e9bd19c09fe-638d757d921e45b7a21b35b61b03a0ddFakePre-f0814689372e48beba579e408cfeffef-e349df4a2a8c42c2b61733708680c3c7FakePre-a4160faf46eb44a6b24db430083b2768-d4c6a8bac073443e9a750723d46aa704FakePre-f65bfee59a0a48bfad87d3cd11cd37ca-f1ed653972be41b29d478d64388040b2FakePre-8429efd19d064f1ba5a222cc1ccaca28-3ed7fb3a7f0445b68a9a21292f96aab8FakePre-36635ae2f0bc4261b1977e0e9f5bcb07-7d0fe839f2204f219712d81f08d305afFakePre-1b291fc5a67d4ab0a46b270de929d066-eb34ba8f3c724a829a00028ebed82153FakePre-e9d589accc864ac1b2ed3f008f8c8c47-bef8c1631e664beba525daa6f7903ebcFakePre-94625ded19fe4588a429f52b22b94bad-72d799acdad34aa2b7554d582b8dec7fFakePre-d700264b7e424e7ba387fe746ad93acb-43d3a092b92a4bfd994efca29ddfc835FakePre-9e1e78c977804dc5bb52460d226f68ee-89469bbe430e49afa67cde110af67754FakePre-12cbd2b908ca4b6087542cd7141c1615-f0d5ab68f02e4ec1b9fd8fc2817cd31dFakePre-40f206d9029c466e94c5691b69ccb1df-b5e0791e8595498bb76ed2f318c5aed9FakePre-c13b1001e2f0402888707f86e9583b6e-d59c3e8c7a1444d4893037b7b1a0070a std::cout << " " << userName << "!" << std::endl;FakePre-18011433a2a44c33ae940e9e0aced933-f56855d11a5141eab7901c8013e0e67c
Task 4 – Reference Highlighting
This new feature improves the readability of code. If you place the text cursor on a symbol, all its instances in the file will be highlighted. Moreover, if two symbols with the same name that belong to different scopes – one local and one global – appear in the same file, the IDE will recognize which one needs to be highlighted. This means that you no longer have to invoke Find All References if you are simply looking for symbols within a file.
In the Greetings.cpp file, click on the greetingsType argument of the Greetings constructor.
Notice that greetingsType is highlighted either in the method arguments or in the method body; also notice that the greetingsType class member is not highlighted.
Figure 15
Reference highlighting
In the Show function, click any occurrence of cout. You can use CTRL+SHIFT+DOWN and CTRL+SHIFT+UP to move between the highlighted references.
Figure 16
Navigating between the highlighted references
Task 5 – Semantic Colorization
Semantic colorization helps you quickly scan code and infer more semantic meaning through enhanced visual feedback in the editor. In addition to the keywords, strings and comments, other tokens like types, enumerations and macros are colorized; the parameters are in italics and so on. However, there are even more colorization options available for each of the language elements that you will now explore.
In the new Quick Launch box in the top-right corner of the Visual Studio Window, type font to find the options to change the fonts and colors. Select Environment->Fonts and Colors to open the Visual Studio options dialog box.
The quick launch box is a new Visual Studio feature that can be used to search and execute any command you need. You do not need to look through all the menus or memorize shortcuts to find them.
Figure 17
Use the Quick Launch to quickly find and execute commands
In the Display Items list, select C++ Fields and set the Item foreground to Purple.
There are only a few tokens that are colorized differently by default. However, notice that around twenty different semantic tokens are available to users as shown in the screenshot below.
Figure 18
Customizing the fonts and colors
You can also select different options in the Show settings for list. For instance, you can customize the printed code style, reducing the font size and removing the colorization. If you want to restore the original styles, click Use Defaults.
- Select C++ Member Functions under Display Items and select Bold.
- Select C++ Namespaces under Display Items and set the Item foreground to Gray.
Click OK and switch back to the code. Notice how the fields, functions and namespaces stand out to facilitate code readability.
Figure 19
Semantic colorization
- You can go back to the default values by re-opening the Fonts and Colors options page and clicking Use Defaults.
You can customize your IDE to colorize the tokens differently. For example, you can choose to colorize local and global variables differently, which could be a handy source when differentiating between variables that are identically named but defined in different scopes.