Discovering C++11

It seems that while I was off in the land of C# and C++/CLI, something really awesome happened in the C++ world. The C++0x standard was ratified and became C++11. Not only that, but there seems to already be wide support for the vast majority of features in modern compilers!

I think C++11 puts C++ much closer to the productivity levels of more modern programming languages. This is something that C++ desperately needed to stay relevant for more than just “performance oriented” programming.

Changes to the language are too vast to write about in their entirety, and since I’m late to the party, most of the best bits have already been covered extensively. However, I’d like to go over a few of the features that I have found vastly boost my productivity.

The improved auto keyword

We can now use auto in place of very long type names. The compiler figures out the type from the expression and everything stays type safe.

//instead of
std::vector<std::string>::iterator start = vecOfStrings.begin();

//we can now write:
auto start = vecOfStrings.begin();

Range based for loops

We can more easily iterate through C++ collections

MyClass::iterateThings()
{
	std::vector vecOfStrings;
	vecOfStrings.push_back("Hello");
	vecOfStrings.push_back("my");
	vecOfStrings.push_back("name");
	vecOfStrings.push_back("is");
	vecOfStrings.push_back("C++11!");

	for (auto sayStr : vecOfStrings) {
		std::cout << sayStr << " " <<;
	}
	//instead of
	for (std::vector<std::string>::iterator i = vecOfStrings.begin(); 
		i != vecOfStrings.end(); ++i) 
	{ 
		std::string& str = (*i); //... 
	} 
}

Lambda Functions

We can define small, one off functions that perform a task at the call site rather than away from it

void function() {
	std::vector numbers;
	numbers.push_back(1);
	numbers.push_back(2);
	numbers.push_back(3);

	std::for_each(numbers.begin(), numbers.end(), 
		[](int i){ std::cout << ' ' << i; });
}

//
//instead of this
//
struct funky {           // function object type:
  void operator() (int i) {std::cout << ' ' << i;}
} functor;

void function() {
	std::vector numbers;
	numbers.push_back(1);
	numbers.push_back(2);
	numbers.push_back(3);

	std::for_each(numbers.begin(), numbers.end(), functor);
}

There are many more features present in C++11 that are worth your time. If you haven’t used C++ in a while and you have a project that demands staticly typed, native object oriented programming, C++11 is worth a close look.

2 thoughts on “Discovering C++11

  1. Pingback: C++11 Reference Capture | I am David Daeschler

  2. Pingback: Asynchrony and C# | I am David Daeschler

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s