Jump to content

[Discussion] -std:C++11


Recommended Posts

Hi again guys.

We've already discussed the obvious performance improvements that can be gained with using the latest C++ standard: C++11. GCC and VisualC++ compilers both support it and it would make sense to use it.

The main gains are:

1. Faster ranged-for based loops. These are fast and convenient loops designed to loop over containers that have ::begin() and ::end() iterators. You can easily make your own code also work with ranged-for, by supplying these two functions.

The loop definition and its underlying implementation provide optimization that would be tedious and ugly to do in actual code.


for(SVertex& vert : m_Vertices)
{
vert.m_Position.Z += deltaHeight;
}

2. The 'auto' keyword. This should be pretty self explanatory, it makes awful code look pretty:


std::map<int, std::string> dictionary;
// C++03:
for(std::map<int, std::string>::iterator it = dictionary.begin(); it != dictionary.end(); ++it)
{
// ...
}
// C++11:
for(auto it = dictionary.begin(); it != dictionary.end(); ++it)
{
// ...
}

3. Smart pointers and new STL containers. A lot of boost implementations have been brought over to C++11, including: shared_ptr<>, unique_ptr<>, map<>, unordered_map<> and more. It would make sense to use the implementations from the standard library now.

4. Lambdas. These useful in-line functions are very handy for small functions that rely on callbacks:


std::vector<int> v;
std::for_each(v.begin(), v.end(), [](int n) {
std::cout << n << std::endl;
});

5. Static assert. Before we would have to rely on some boost implementation, but now the standard provides us a neat way of ensuring template code correctness compile-time:


template<class T> class Container
{
static_assert(sizeof(T) >= 4, "Specified T is not big enough for this specialized container!");
// ....
};

6. Move semantics. The newly introduced move constructor and move operator would give us a noticeable increase in performance because of the liberal use of std::string and std::vector copying. Since C++11 STL has implemented move semantics, we can avoid doing full-copy of temporary objects.

You can read more about C++11 here: http://www.codeproje...splus-Developer

As for enabling C++11 features:

  • GCC - Just add the compiler switch -std=c++11 and we're good to go
  • VisualC++ - Use Visual Studio 2010 or higher.

Tell me what you think guys :) This is a small step for our compiler script, but a huge leap for pyrogenesis.

Edited by RedFox
Link to comment
Share on other sites

Not all the features of C++11 are implemented in VS2010 or the standard library that ships with VS2010. To make proper use of the C++11 features you must compile with VS2012. Although not even 2012 is feature complete but we probably won't be using things like proper variadic templates etc.

I think this is a no brainer for the code base though, in my opinion the highest priority should be implementing move constructors. We get the free upgrade of the STL using std::move(), std::forward and emplace_back() but we still need to implement the move constructors in our own classes.

If you're not up to reading about C++11 the following is a fantastic video on C++11 by Bjarne Stroustrap.

http://channel9.msdn...rup-Cpp11-Style

And there are a number of other fantastic lectures from that conference on the various new features.

Edited by Thanduel
Link to comment
Share on other sites

Very interesting video, thanks for sharing!

I think we should go for it. C++11 offers some new features to make code easier to understand and more maintainable and maybe even brings some performance improvements. I don't see any disadvantages at the moment.

Link to comment
Share on other sites

I say do it, as long as GCC, clang, and VS2010+ support it. I tend to strongly dislike C++ for a number of reasons, but IMO C++11 is a big improvement, especially with auto, lambdas, and the smart pointer stuff. Also, move constructors could definitely give a speed improvement, albeit probably a fairly smalI one. I would be slightly hesitant to move if only VS2012 supports it; a number of people including myself are still on VS2010. Additionally, I feel like clang is a better compiler than GCC and we should try to maintain compatibility with it. (Although I'm not sure, do we even support clang currently?) Whoever maintains these forums: it's a real pain to post with mobile Firefox. The editor is incredibly finicky; it won't even let me insert a new line, among other things. :(

Link to comment
Share on other sites

This is a huge change, we're basically dropping support for lots of older compilers if we do this, and I don't know if the gains offset that. Syntax is a small thing really, people have been reading ugly C++ syntax for decades, it won't kill us. Boost is a dependency of the game, so unless C++11 adds everything we need from Boost (unlikely), does it really matter if C++11 adds a few of them?

So it seems to boil down to performance, can we get some measurements of C++ boosting the game's performance in practice?

Also let's make a list of known compilers that do and don't support this, and which OSes will be affected. Maybe we can get Philip to provide updated usage stats of the game and see how many people use those OSes (approximately anyway).

Link to comment
Share on other sites

http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport

The above link shows the support for c++11. All the major compilers (including clang) support the perf related features we want to use. Vc++ stl maintainer estimates a 20 percent perf improvement just from move constructors. Below is a good explanation of rvalue references and move constructors. This is certainly not just a matter of making the code look pretty.

http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-9-of-n

Link to comment
Share on other sites

Also c++11 is backwards compatible with c++03 etc.

Finally OS support shouldn't be an issue the only potential os problem is FreeBSD and they are moving to use clang now instead of the old GCC 4.2 version they were using due to licensing issues.

EDIT:

I found a list of VC2010 supported C++11 features:

http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx

Unfortunately range based for loops are not in that list.

Edited by Thanduel
Link to comment
Share on other sites

  • 3 weeks later...

Are there any more thoughts on this or does this thread need some benchmarks to move forward?

I've seen mention of concern about C++11 leading to dropping support for certain platforms. Using C++11 will make no difference to the end user aside from the free performance improvements, it will not change platform compatibility for the end user. As I've mentioned above all the major compilers support C++11 I'm not sure what older compilers are worth mentioning however as may be seen in the link given above even the intel compiler supports C++11. The only potential restriction would be to use only the features supported by MSVC 2010.

The use of Boost makes no difference, using C++11 with Boost will improve Boost's performance. The latest versions of Boost make use of C++11 features conditionally on the compilers support and the C++03 versions will compile with a C++11 compiler.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   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.

 Share

×
×
  • Create New...