Channels ▼
RSS

C/C++

New C++ Language Features in Visual Studio 2013


Visual Studio 2013 includes lots of useful improvements for Visual C++, including support for many ISO C++11 core language specifications and some ISO C++14 library features. In this article, I illustrate and explain the new supported language features VS2013 has added: raw string literals, brace initialization, initializer list constructors, and delegating constructors. A second article will cover the most of the remaining features of importance.

Special Characters and Raw String Literals

If you've worked with C#, you are probably familiar with the @ string literal that makes it easy for you to avoid having to escape special characters. The ISO C++11 core language specification defines raw string literals that provide a similar feature, and Visual Studio 2013 has added support for it: When you define strings that include paths, URIs, HTML, JSON, XML, or regular expressions, having to escape special characters makes it difficult to read and understand the original content. The support for raw string literals removes the need to escape special characters and allows you to define the string with the original content.

The following lines show JSON content that requires escaping many special characters if you use the classic string:

{
   "Title":"C/C++",
   "Subtitle":"Powered by C/C++",
   "Description":"The world of C/++ developers",
   "MainPage":"cpp",
   "Items":null,
   "Id":"6"
}

The following code escapes all the special characters used to define the previously shown JSON content. Note that it is difficult to decode the JSON content with all the key-value pairs in a single line of code and all the escaped special characters:

auto json_content = "\n{\n    \"Title\":\"C/C++\",\n    \"Subtitle\":\"Powered by C/C++\",\n    \"Description\":\"The world of C/++ developers\",\n    \"MainPage\":\"cpp\",\n    \"Items\":null,\n    \"Id\":\"6\"\n}";

In Visual Studio 2013, the following lines use raw string literals to define the previously shown JSON content. Notice that it is possible to embed double quotation marks and use newlines. This way, you can easily read and understand the JSON content. The code makes json_content a const char* that contains exactly the same value generated with the previous line that used the escaped special characters.

auto json_content = R"(
{
    "Title":"C/C++",
    "Subtitle":"Powered by C/C++",
    "Description":"The world of C/++ developers",
    "MainPage":"cpp",
    "Items":null,
    "Id":"6"
})"; 

You might notice the use of R"( as a prefix and )" as a suffix that weren't part of the original JSON content. The prefix and suffix represent and delimit the raw string literal. In fact, the generic way of representing raw string literals is R"ddd(characters) ddd"; where ddd is a delimiter and characters are the string characters. These characters can contain any graphic character, including the double quotation mark ("), backslash (\), or newline character. You can have either raw narrow string literals (const char*) or raw wide string literals (const wchar_t*). Raw narrow string literals start their definition with R" and raw wide string literals use LR".

The delimiter is an optional user-defined sequence of up to 16 characters that immediately precedes the opening parenthesis of the raw string literal and also immediately follows its closing parenthesis. You might need to use a delimiter when you have to define strings that contain double quotation marks and parenthesis, a situation that arises frequently when working with JSON content.

For example, the following lines generate compiler errors because the raw string literal has JSON values that include double quotation marks and parenthesis:

auto json_content = R"(
{
    "Title":"(C/C++)",
    "Subtitle":"(Powered by C/C++)",
    "Description":"(The world of C/++ developers)",
    "MainPage":"cpp",
    "Items":null,
    "Id":"6"
})";

The use of ZZZ as a delimiter in the following lines solves the problem and allows you to embed double quotation marks and parenthesis in the raw literal string:

auto json_content = R"ZZZ(
{
    "Title":"(C/C++)",
    "Subtitle":"(Powered by C/C++)",
    "Description":"(The world of C/++ developers)",
    "MainPage":"cpp",
    "Items":null,
    "Id":"6"
})ZZZ";

Here is a sample regular expression that matches the opening and closing pair of any HTML tag.

<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>

If you don't use raw literal strings, you won't be able to see the same string, as shown in the next line:

auto regular_expression = "<([A-Z][A-Z0-9]*)\\b[^>]*>(.*?)</\\1>";

However, if you use a raw literal string as in the next line, you will be able to read exactly the same regular expression without escaped special characters:

auto regular_expression = R"(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>)";

The support for raw literal strings will definitely make a difference when you write and maintain code that works with regular expressions and Web-related content.

Working with Brace Initialization and Initializer List Constructors

Visual Studio 2013 allows you to use brace initialization without the equals sign for any class, struct, or union. The default brace initialization with empty braces is possible when the type has a default constructor that might be implicitly or explicitly declared.


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.
 

Comments:

alariq
2014-02-06T18:31:13

I see your point.
Thanks for the article!


Permalink
GastonHillar
2014-02-06T01:00:49

Sergii,

Thanks for your comment.

You're absolutely right. There are simpler implementations of the constructor as you mention in your example.

However, my idea in the example was to provide a very simple sample piece of code where you can easily understand how the different constructors are chained, and therefore, I used the simple chained initialization case, probably the simplest possible case. Obviously, you can use a single constructor for this case as you mention and that's all. However, try to imagine that the case is a bit more complex and requires independent and chained constructors, so that taking advantage of delegating constructors makes sense.

The goal of this article is to focus on the different features with simple examples and then you can combine all these new features with the best practices and your experience to have a better coding experience with the new features introduced in Visual Studio 2013.

In fact, many of the new C++11 initialization features explained in the article also provide mechanisms to create a simpler constructor for the example. However, I do believe simple examples go straight to the point and make it easier to understand the new features and then apply them in the most complex cases.

In addition, the idea is to explain the features that I considered most useful for the most common situations without using huge amounts of code that would make the article difficult to read.

Cheers,

Gaston Hillar


Permalink
alariq
2014-02-05T11:19:23

Maybe it is just not the best example for "
Reducing Duplicate Code with Delegating Constructors". Because one needs no more than 1 constructor to implement:

screen_point::screen_point(double x=-1, double y=-1, double alpha=-1)
:m_x(x)
,m_y(y)
,m_aplha(alpha),
,m_incomplete_point(alpha==-1)
{
}

Or do I miss anything?


Permalink

Video