How should I approach programming in C ++ with the new C ++ 11 standard?

I have been programming C ++ for a while, and I'm used to doing things like:

vector<int> vi; for (vector<int>::const_iterator it = vi.begin(); it != vi.end(); ++it) { // do something with it } 

However, the new C ++ standard, C ++ 11, introduces the auto keyword, so I can write things like:

 vector<int> vi; for (auto it : vi ) // do something with it } 

Should I use this in my code, or should I use an iterator approach?

The same question applies to many other things in the new C ++ 11 standard. Should I start using new things in my code and forget about the old ways of doing things?

+4
source share
1 answer

It depends on the support of the project and the compiler. C ++ 11 adds a lot of things to make your program more readable, faster, more reliable, etc.

The only drawback of C ++ 11 is that not all platforms support it. Project requirements may provide support for platforms or compilers that do not support C ++ 11, or the value obtained by switching to C ++ 11 may not be as strong as what you lose by abandoning compilers or platforms that do not missing C ++ 11 support.

This is not an all-or-nothing solution. You can choose some functions that will work for you and decide to leave some others until more of your compilers get better support.

Different projects will be different, and the judgment will depend on how much you value the various benefits brought by C ++ 11, and how you value support for older platforms. Over time, everything will develop further in favor of C ++ 11, until there are no questions, but for now you just need to look at your projects and decide whether and what parts of C ++ 11 are right for you.

+2
source

All Articles