Channels ▼
RSS

C/C++

More New C++ Features in VS2013


In the first installment of this two-part series on C++ enhancements in Visual Studio 2013, I explained the use of some new C++11 core language features: raw string literals, brace initialization, initializer list constructors, and delegating constructors, among others. In this article, I explain defaulted and deleted functions, explicit conversion operators, default template arguments for function templates, alias templates, and transparent operator functors. For ease of coding, though, I'll start with the new code formatting settings provided by the IDE.

Configuring the New Code Formatting Settings

With Visual Studio 2013, Microsoft finally added dozens of code formatting settings to permit more control over how C/C++ code appears. You can use these settings to define configurations such as the desired indentation, spacing, line wrapping, and the new-line placement of braces and keywords. Visual Studio automatically formats code that you paste into a file (and whenever you complete statements and blocks) per your preferences. Before you start working with C/C++ code in Visual Studio 2013, it is a good idea to spend some time reviewing the formatting settings. Just select Tools | Options | Text Editor | C/C++ | Formatting, and explore the following nodes with the diverse settings (see Figure 1):

  • General
  • Indentation
  • New Lines
  • Spacing
  • Wrapping

When you activate an option, the Options dialog box will display a preview of how the sample code format changes when you apply that new setting (see Figure 1). After you adjust the settings, the new formatting options are applied only to the new code you type or paste: Your existing code won't change. However, if you want a piece of your existing code to use the new settings, you simply need to cut and paste it. The IDE will apply the new code formatting settings when you paste, and you will be able to easily check whether the configuration options are exactly what you want for your code.

VS2013 and C++
Figure 1: The Options dialog box displaying a sample code preview with the "Indent Braces" code formatting option activated.

The "New Lines" settings that define the position of open braces allow control of the desired position when you have:

  • Namespaces
  • Types
  • Functions
  • Control blocks
  • Lambdas

In all the cases, you can activate the "Don't automatically reposition" option when you want to avoid a change in the open brace in a particular situation. These settings are very useful for achieving code-formatting objectives (see Figure 2).

VS2013 and C++
Figure 2: The Options dialog box displaying a sample code preview with the "Keep on the same line, but add a space before" setting of the "Position of open braces for functions" option activated.

In addition, Visual Studio 2013 includes a brace completion features. Whenever you enter C/C++ code, the IDE auto-completes the closing characters for the following opening characters:

  • Curly brace ({)
  • Double quote (")
  • Single quote (')
  • Square bracket ([)
  • Opening parentheses (()

The IDE also completes parenthesis for newly supported raw string literals, adds the semicolon (;) for class types, and completes multiline comments. For example, if you enter the following line of code in the IDE:

class screen_point {

Visual Studio 2013 will add the closing character for the curly brace and a semicolon. However, the cursor will remain in position (where you are writing the code). This way, if you press Enter after the previously shown line, your auto-completed code will look like this:

class screen_point {
};

After spending some time reviewing all the new settings, you will definitely enjoy the new code formatting options and the auto-completion features. However, one of the most exciting features you will find in Visual Studio 2013 when working with C/C++ projects is that the Project Properties window is now resizable (after so many years with the unacceptable fixed size).

Requesting Defaulted and Deleted Functions

Visual Studio 2013 adds partial support for defaulted and deleted functions. When you declare your own constructor in C++, by default, the compiler prevents the generation of the default version. However, sometimes you want to preserve the generation of the default constructor even when you specify one or more constructors. The ISO C++11 core language specification defines a way to keep the generation of the default functions; and Visual Studio 2013 has partial support for this feature.

Now, you can explicitly request the default implementation of a constructor from the compiler. For example, the following lines use = default to request the compiler to implement the default constructor for the screen_point class, even when there is another constructor declared. In Visual Studio 2013, you cannot use = default to request member-wise move constructors and move assignment operators because these defaulted functions are not yet supported.

class screen_point {
	public:
		screen_point() = default;
		screen_point(double x) : screen_point() {
			m_x = x;
		}
		double m_x{ -1 };
};

You can achieve the opposite effect of the default keyword by using the delete keyword. When you use delete, you indicate to the compiler that the function doesn't have any implementation and cannot be called. Thus, you can disable the methods that the compiler would automatically generate when you don't provide your implementations. For example, the following lines use = delete to request that the compiler disable the default functions that allow copying screen_point class instances.

class screen_point {
	public:
		screen_point() = default;
		screen_point(double x) : screen_point() {
			m_x = x;
		}
		screen_point(const screen_point&) = delete;
		screen_point& operator=(const screen_point&) = delete;
		double m_x{ -1 };
};

Working with Explicit Conversion Operators

Visual Studio 2013 includes support for explicit conversion operators. You can declare conversion operators explicit in order to prevent unwanted implicit conversion. For example, the following lines show a very simple example of using the explicit keyword in the screen_point2 class to explicitly declare the conversion operator to screen_point1:

class screen_point1 {
	public:
		screen_point1(double x) : m_x(x) { }
		double m_x;
};

class screen_point2 {
	public:
		screen_point2(double x) : m_x(x) { }
		explicit operator screen_point1() { return{ screen_point1(m_x) }; }
		double m_x;
};

The following lines will trigger the use of the explicit operator conversion to convert the screen_point2 instance (point2) into a screen_point1 instance:

screen_point2 point2 = 45.5;
screen_point1 point1 = (screen_point1)point2;

Because the operator conversion uses the explicit keyword, the following line won't compile. (The compiler will throw "error C2440: 'initializing' : cannot convert from 'screen_point2' to 'screen_point1'.")

screen_point1 point3 = point2;

This way, you can prevent an unwanted implicit conversion from happening. Just to see the difference, if you remove the explicit keyword to the operator declaration, as shown in the following line, the previous line will compile.

operator screen_point1() { return{ screen_point1(m_x) }; }


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video