Three New Features in C++11
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.
2 Comments
Recommended Comments