I never saw it before and I am wondering.
'auto" keyword is a new directive in C++11 standard. It allows compiler to deduce the type of variable automatically. It makes code more clear and easier to write, especially when the type itself is quite complex (like boost::function, shared_ptr or nested containers).
C++11 auto is quite useful in decreasing size of type definitions:
for (std::vector<ConstTransitivePtr<CGTownInstance> >::const_iterator i = state->towns.begin(); i < state->towns.end(); ++i)
for (auto & i = state->towns.begin(); i < state->towns.end(); ++i)
C++11 with range-based for loop
for (auto & i : state->towns)
(Note that due to gcc 4.5 this can’t be used in vcmi right now - use BOOST_FOREACH instead)
I do not use C++ on regular basis and was wondering how bad is my c++ if I had troubles descifering:
auto takeStack = &](std::vector<const CStack *> &st) -> const CStack*
It’s lambda expression (in practice, function object) and yes, it has terrible syntax.
Here’s some pictured desciption of these.
What’s so terrible in this syntax? I think it’s quite OK taking into account specific needs of C++. The real shame is that they are not polimorphic (or template-based actually, as functional polimorphism suggests some kind of type safety).
It’s beatiful, if you consider the equivalent lambda-free code.
This is being worked on: isocpp.org/blog/2012/12/an-imple … aisal-vali
Given some luck, we’ll have them in C++14. Then we’ll just have to wait, till we can require GCC 4.9 to build VCMI.
As for real shame, I’d say the worst are repetitive serialize() methods. (causing errors from time to time, when not properly updated) Compiler perfectly knows what class members are, if we just could use that…
VCMI currently allows C++11 features that are both supported by GCC 4.5 and Visual Studio 2010 ( wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport ) . That includes:
- type inference with “auto” and “decltype” keywords
- lambda expressions
- r-value refs and move semantics
- new function declaration syntax with trailing return type
I guess that should be added to wiki somewhere.
Done. Together with listing of features that would be available after quite reasonable compiler update – it’s pretty impressive. Next Debian stable with GCC 4.7 should be released early next year, so it would be a reasonable requirement then.
Current Ubuntu LTS comes with gcc 4.6. I’d like to keep it supported till next LTS release (april 2014).
2013 - gcc 4.6
2014 - gcc 4.7
Ok, I’ve corrected it – override, final and delegating constructors are out.