Jump to content
  • entries
    944
  • comments
    5,899
  • views
    929,697

Three New Features in C++11


Josh

1,770 views

 Share

C++11 modernizes the C++ programming language with many new features and techniques. Below are just a few of the new ways you can use C++ with Leadwerks.

 

auto

You can automatically declare a new variable type by the data that is assigned to it:

auto i = 42;	 // i is an int

auto l = 42LL; // l is an long long

auto p = new foo(); // p is a foo*

 

Here's a more practical use that saves a lot of typing. The code below:

std::map<std::string, std::vector<int>> map;

for (std::map<std::string, std::vector<int>>::iterator it = begin(map); it != end(map); ++it)

{

}

 

Can be replaced with this:

std::map<std::string, std::vector<int>> map;

for (auto it = begin(map); it != end(map); ++it)

{

}

 

std::next

You can retrieve the nth element of a standard list with the new std::next function. For example, I use this internally in the engine to retrieve a spotlight or other entity by index:

return *(std::next(spotlights.begin(), n));

 

Shared Pointers

C++11 supports unique pointers, which are like regular pointers we use now. It also supports shared pointers, which use automatic reference counting, and weak pointers, which do not increment the object reference count. This basically provides built-in functionality similar to the reference counting system Leadwerks uses. Future major versions of the engine may make more heavy use of this feature.

 

And one bonus feature: nullptr

The new nullptr value is not interchangeable with NULL, which can convert to an integer. This makes it easier to specify that a value is supposed to be a pointer and that zero is not a valid input.

 

You can read more about new C++11 tricks and features here.

 Share

2 Comments


Recommended Comments

Ranged based for loops clean up that iterator business even more! Also lambdas clean up the algorithms in STL(stuff like std::sort). My favorite is probably std::bind, been using it for years from Boost but it's nice to see it in the standard.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...