May 2014

Quick Q: How do generic lambdas work? -- StackOverflow

Quick A: They generate a templated operator(), and that means you can call the same closure object with all sorts of different parameters and it just stamps out the function for each set of parameter types.

A "new classic" question on SO, classic because it was posted a year ago, but newly relevant because now the mainstream compilers are implementing the feature:

How does generic lambda work in C++14?

How does generic lambda work (auto keyword as argument type)?

Is it similar to templates where for each different argument type compiler generates functions with the same body but changed types or is it more similar to Java's generics?

Code example:

auto glambda = [](auto a) { return a; };

Fast Polymorphic Collections with Devirtualization -- Joaquín M López Muñoz

munoz.pngStill on the theme of "contiguous enables fast," a followup on the recent "Fast Polymorphic Collections" article:

Fast Polymorphic Collections with Devirtualization

by Joaquín M López Muñoz

From the article:

The poly_collection template class we implemented for fast handling of polymorphic objects can be potentially speeded up in the case where some of the derived classes it manages are known to the compiler at the point of template instantiation...

Allowing for the specification of a list of statically known derived class types as part of a poly_collection instantiation opens up possibilities for internal performance optimizations that do not impact the usage interface of this template class. The actual improvements achieved depend very much on the compiler being used, but in general are greater when passing polymorphic functors (such as generic lambdas) to for_each and qualifying virtual functions as final where possible.

Introducing emplacer -- Tim

Another nice way to have a polymorphic collection without sacrificing that performance-precious contiguous memory layout:

Introducing emplacer: allocate subtypes of an abstract base class directly in a container

by Tim

From the article:

So I complained to my friend Jeremy that C++ should be able to do this. This isn't Java, I shouldn't have to do a separate allocation for each object...

As alluded to earlier, you can also use it in containers. Here's an example:

std::vector<ShapeAny> shapes;

shapes.emplace(shapes.end())->emplace<Rect>(3, 4);
shapes.emplace(shapes.end())->emplace<Square>(3);

//emplace returns reference to *this, so you can use it like this too
shapes.push_back(ShapeAny().emplace<Rect>(5, 6));
shapes.push_back(ShapeAny().emplace<Circle>(1));

for (auto shape: shapes) {
  std::cout << shape->area() << std::endl;
}

CppCon 2014: Initial partial topics and speakers, talk submission deadline

cppcon-120.pngCppCon is now just 120 days away! The final program is expected to be available in late June, but in the meantime the organizers have shared a sneak peek at a partial list of speakers and topics for this year's event.

Program Update, Submission Deadline Reminder

by Boris Kolpackov

Speakers proposing talks so far include a nice mix of speakers -- not only the most widely known veteran C++ speakers and many active standards committee members, but also a range of speakers from industry and specific domains, and from all around the world forming a truly international cast:

  • Alan Uthoff
  • Andrew Stepanchuk
  • Bjarne Stroustrup (keynote)
  • Boris Kolpackov
  • Brett Hall
  • Camille Coti
  • Edouard Alligand
  • Eric Niebler
  • Herb Sutter
  • Jason Turner
  • Jens Weller
  • Joel Falcou
  • John Farrier
  • Jon Kalb
  • Kate Gregory
  • Marshall Clow
  • Michael Caisse
  • Michael Wong
  • Nate Kohl
  • Robert Ramey
  • Roland Bock
  • Pedro Ramalhete
  • Peter Sommerlad
  • Scott Meyers
  • Sridhar Poduri
  • Stefanus Du Toit
  • Steve Heller

That's just the start, and is already more than 3x the number of speakers there were at GoingNative.

From the talks submitted so far, we are seeing several popular topic themes, with multiple sessions in each of the following themes (with more themes to come):

  • new Standards Committee proposals for features and libraries
  • how to write better and more modern C++ code or make the compiler help you do that
  • developing for mobile platforms
  • writing reliable and exception-safe code
  • concurrent and lock-free programming
  • database access and object persistence
  • continuous delivery integration
  • various memory models (shared, user mode virtual, and transactional)

Do you have a topic you could speak about for anything from 30 min to 3 hours? Submit a session proposal! The talk submission deadline is still open for at least another week. From the article:

While we already have some great content, we need more, so don’t forget to submit your proposal before the deadline, May 15. You only have one week left! To make the submission process smoother there is also the new, easier to use submission form.

Super Early Bird registration sold out, but Early Bird registration will be open until June 30. We look forward to seeing you with us in the hot crowd at this year's C++ fest in the beautiful Seattle area this September.

Preface to Programming: Principles and Practice Using C++, 2nd Edition -- Bjarne Stroustrup

programming-2e.jpgStroustrup's introduction to programming book has now been updated for modern C++. The Preface was posted on InformIT:

Preface to Bjarne Stroustrup's Programming: Principles and Practice Using C++, 2nd Edition

by Bjarne Stroustrup

From the preface:

This book is for someone who has never programmed before but is willing to work hard to learn. It helps you understand the principles and acquire the practical skills of programming using the C++ programming language. My aim is for you to gain sufficient knowledge and experience to perform simple useful programming tasks using the best up-to-date techniques. How long will that take? As part of a first-year university course, you can work through this book in a semester...

Dangerous int-to-string conversions -- Andrzej Krzemieński

Note that the article doesn't mention to_string, which is the preferred C++11 way to convert an int to a string. But it does point out an issue -- you can assign an int to a string, including by accident.

Dangerous int-to-string conversions

by Andrzej Krzemieński

From the article:

And now, because int is implicitly convertible to char, our unexpected conversion from int to std::string ‘works’ (under some definition of ‘work’)...