Channels ▼
RSS

Tools

More New C++ Features in VS2013


Working with Default Template Arguments for Function Templates

In previous Visual Studio versions, you could provide default values for class template parameters, but not for function templates. Visual Studio 2013 adds support for default template arguments for function templates. For example, the following lines show a simple example of using of this C++11 feature to specify the default template argument for the talk function template. Notice that only one argument has a default specified.

template <typename T> class cat {
	public:
		void write_code(T t) { }
};

template <typename T> class tail {
	public:
		void write_code(T t) { }
};

template <typename T, typename U = cat<T>> void talk(T t) {
	U u;
	u.write_code(t);
}

Table 1 shows the types inferred for each line of code in this example.

Code

Inferred template type

talk("Good morning!");

talk<const char*, cat<const char*>>

talk(300L);

talk<long, cat<long>>

talk<long, tail<long>>(150L);

talk<long, tail<long>>

Table 1: Types inferred for each line of code in the sample.

Using Alias Templates

Visual Studio 2013 supports alias templates. They enable you to create an alias for a template providing only the necessary template arguments. For example, the following lines define the string_tuple alias template for the tuple template with the first value type set to string.

#include <string>
using namespace std;

template<typename T>
using string_tuple = tuple<string, T>;

string_tuple is a template and it is possible to use it (as shown in the following line) to declare a string_tuple<double>. Alias templates allow you to simplify your code.

string_tuple<double> string_double_tuple;

Using Transparent Operator Functors

Visual Studio 2013 still doesn't implement all ISO C++11 features, only those that the Visual Studio team considers most useful for developers (which explains why Visual Studio 2013 started adding a few ISO C++14 standard library features even when ISO C++11 compliance isn't complete).

Visual Studio 2013 includes support for the ISO C++14 standard library transparent operator functors. Table 2 shows the transparent operator functors and their related operators:

Transparent functor

Operator

bit_and<>

Operator &

bit_not<>

Unary operator ~

bit_or<>

Operator |

bit_xor<>

Operator ^

divides<>

Operator /

equal_to<>

Operator ==

greater<>

Operator >

greater_equal<>

Operator >=

less<>

Operator <

less_equal<>

Operator <=

logical_and<>

Operator &&

logical_not<>

Unary operator !

logical_or<>

Operator ||

minus<>

Operator -

modulus<>

Operator %

multiplies<>

Operator *

negate<>

Unary operator -

not_equal_to<>

Operator !=

plus<>

Operator +

Transparent operator functors are extremely useful for simplifying your code. For example, you can use std::less<>, std::less_equal<>, std::greater<>, or std::greater_equal<> to easily call the std::sort function without having to create your own functor, function, or lambda. The following lines show a simple example that uses std::sort to sort a std::vector<int> with 20 int elements and then display the sorted vector elements in the console.

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> v = { 15000, 3500, 17500, 50, 4500, 200, 150, 17000, 350, 20, 5, 14000, 4000, 30, 40, 10, 5500, 80, 19500, 2350 };

	std::sort(v.begin(), v.end(), std::greater<>());

	std::cout << "Sorted vector elements:";

	for (auto it = v.cbegin(); it != v.cend(); ++it)
		std::cout << *it << '\n';
	std::cout << '\n';

	return { 0 };
}

The call to std::sort uses the std::greater<> transparent operator functor for operator >, included in <functional> as the third argument. The next line shows the simple and easy to understand call to std::sort. You don't need to read the code within a lambda, function, or functor to know that the sort operation will put the greater values first.

std::sort(v.begin(), v.end(), std::greater<>());

When you run the sample, you will see the following output in the console:

Sorted vector elements:
19500
17500
17000
15000
14000
5500
4500
4000
3500
2350
350
200
150
80
50
40
30
20
10
5

There are many different ways to achieve the same result. However, the new transparent operator functor for operator > provides a generic way that doesn't require you to add additional code (and allows you to change the vector type without having to make any additional edits). For example, if you want to change the vector type from int to double, you don't need to make changes to the line that calls std::sort. You can continue using the same transparent operator functor for operator > (in our example, the third argument remains std::greater<>()). The following line shows the change in the vector type and its elements from int to double. The rest of the code can remain the same and the sort operation will work fine.

std::vector<double> v = { 15000.5, 3500.4, 17500.2, 50.5, 4500.3, 200.4, 150.3, 17000.2, 350.7, 20.2, 5.4, 14000.5, 4000.3, 30.2, 40.1, 10.7, 5500.3, 80.2, 19500.4, 2350.3 };

Another possible way of calling the std::sort method with a std::vector<int> is to use a lambda. The following line uses a lambda instead of std::greater<>() as the third argument:

std::sort(v.begin(), v.end(), [](int a, int b) { return (a > b); });

If you change the vector type from int to double, you have to replace the code within the lambda. In addition, it is easier to read std::greater<>() as the third argument. Of course, you can create your own generic functor and you will have the same type-change benefits than when you use the transparent operator functor for operator >. However, I find it a good idea to use everything that is included in the standard libraries, so as not to take on unnecessary additional work.

Conclusion

Visual Studio 2013 has also added support for other C++14 features, including cbegin()/cend(), rbegin()/rend(), and crbegin()/crend(), which are non-member functions in the standard template library. In these two articles, I've covered many of the useful C++ enhancements in Visual Studio 2013. You will definitely want to take advantage of some of them when you start working in C++ projects with the newest version of Visual Studio.


Gaston Hillar is a frequent contributor to Dr. Dobb's.

Related Article

New C++ Language Features in Visual Studio 2013


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