I love C++11

Things that are not directly connected with Factorio.
Post Reply
kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

I love C++11

Post by kovarex »

So we are just starting to use C++11 in the Factorio sources and I feel like I'v got new set of toys on christmas.

When things like this happen:

Before:

Code: Select all

std::vector<std::string> names;
names.push_back("stone-furnace");
names.push_back("straight-rail");
names.push_back("gate");
BOOST_FOREACH (const std::string& name, names)
After:

Code: Select all

for (const std::string& name: {"stone-furnace", "straight-rail", "gate"})
It makes me happy :)

User avatar
Xecutor
Filter Inserter
Filter Inserter
Posts: 260
Joined: Mon Jun 23, 2014 10:15 am
Contact:

Re: I love C++11

Post by Xecutor »

Yeah, C++11 makes a lot of things much nicer to do.
However, overuse of 'auto' makes sources, while shorter, but harder to read.

Also, is you have proper class Rect with iterator, begin and end, you can write something like this:

Code: Select all

  for(Pos pos:Rect(from,sz))
  {
  ...
  }
instead of

Code: Select all

  Pos pos=from;
  Pos till=from+sz;
  for(;pos.y<till.y;++pos.y)
  {
    for(pos.x=from.x;pos.x<till.x;++pos.x)
    {
    ...
    }
  }

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: I love C++11

Post by kovarex »

Yeah, I would like to avoid overusing auto, as it damages readability.

But in situations like:

Code: Select all

for (const boost::property_tree::ptree::value_type& item: input)
I feel it might be ok.

Also, using the auto for custom structures is also nice, you just need to provide begin/end methods, we use it for iterating of inventories already.

User avatar
Nova
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Mar 04, 2013 12:13 am
Contact:

Re: I love C++11

Post by Nova »

Yes, C++11 is very nice. It's so logical, so pure, so nice... :)
Did ask myself if you already use C++11, but the source code is closed source and i didn't want to ask such a small question because it would maybe annoy you.

Only getting the IDE to work with it was a pain...
Greetings, Nova.
Factorio is one of the greatest games I ever played, with one of the best developers I ever heard of.

Post Reply

Return to “Off topic”