Articles & Books

Quick Q: When should you prefer std::function vs. a virtual function to implement a callback? -- SO

Quick A: Using a named abstract base class makes code more tightly coupled to that specific name. Where possible, prefer using std::function which is more general and flexible.

Recently on SO:

Pros & cons of a callback (std::function/std::bind) vs an interface (abstract class)

I'm creating a server application in C++11 using Boost.Asio. I've created a class, Server, which takes care of accepting new connections. It's basically just:

void Server::Accept() {
  socket_.reset(new boost::asio::ip::tcp::socket(*io_service_));
  acceptor_.async_accept(*socket_,
                         boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
}
void Server::HandleAccept(const boost::system::error_code& error) {
  if (!error) {
    // TODO
  } else {
    TRACE_ERROR("Server::HandleAccept: Error!");
  }
  Accept();
}

I've found two ways (I'm sure there are more) to "fix" the TODO comment, i.e. to move the socket to wherever it should go. In my case I just want it back to the class instance that owns the Server instance (which then wraps it in a Connection class and inserts it to a list).

  1. Server has a parameter in its constructor: std::function<void(socket)> OnAccept which is called in HandleAccept.
  2. I create an abstract class, IServerHandler or whatever, which has one virtual method OnAccept. Server takes IServerHandler as parameter in its constructor and the class instance owning the server instance extends IServerHandler and constructs Server with *this as parameter.

What are the pros and cons of option 1 vs option 2? Are there any better options? I'm having the same problem in my Connection class (OnConnectionClosed). Also, depending on how I decide to design the system, it might need a OnPacketReceived and OnPacketSent callback.

Fun with Lambdas: C++14 Style (Part 1) -- Sumant Tambe

sumant-tambe.PNGA rapid-fire "now write this using lambdas" problem-solution drill with Sumant Tambe:

Fun with Lambdas: C++14 Style (Part 1)

by Sumant Tambe

From the article:

It's common knowledge that Functional Programming is spreading like a wildfire in mainstream languages. Latest promoted languages: Java 8 and C++, both of which now support lambdas. So, let the lambdas begin! and may the fun be ever on your side. The same text is available in slides form on Slideshare. This blog post and the talk/slides are inspired by JSON inventor Douglas Crockford.

Classic C++ Files: Sixteen Ways to Stack a Cat -- Bjarne Stroustrup

For your Friday reading pleasure, here's a classic paper showing off C++'s flexibility with a wink and a smile.

Although it doesn't exercise the new features of modern C++, we believe all of the code still works. How's that for backward compatibility...

Sixteen Ways to Stack a Cat

by Bjarne Stroustrup

From the article:

This paper presents a series of examples of how to represent stacks in a program. In doing so it demonstrates some of the fundamental techniques and tradeoffs of data hiding as seen in languages such as C, Modula2, and Ada. Since all the examples are written in C++ it also demonstrates the flexibility of C++’s mechanisms for expressing data hiding and access.

... The nine-plus cat lives in this paper are dedicated to Dave McQueen who once in desperation proposed the death penalty for presenting "yet another stack example." Also thanks to Andy for giving me a practical demonstration of the difficulty of stacking cats.

Compiler support for C++11 and C++14

C++11 support is still an interesting topic, even that GCC and Clang now fully support it. Also the upcoming C++14 standard is already getting implemented by a lot of compilers. There are two very interesting publications about this topic in recent weeks, first, on italiancpp.org there is a PDF on C++11 and C++14 feature support for Visual C++, Intel, Clang and GCC. Also Just a few days ago, a interesting blogpost about this topic was published on C++Rocks, focussing on compiler and library support:

C++11/14 compiler and library shootout

by C++ Rocks

From the Article:

It’s been almost a year since my last comparison of C++11 support across different compilers, so I decided to take a break from working on my book about C++11/14 features in VS2013, and see how things have changed.

C++ User Group Meetings in March

The montly overview about C++ User Group Meetings:

C++ User Group Meetings in March 2014

by Jens Weller

From the Article:

The full list of all meetings in march:

10.3 C++ UG Malmö, Sweden - C++folk/ C++11 Lightning talks
10.3 C++ UG Philadelphia - boost spirit
12.3 C++ UG San Francisco/Bayarea - Fun with Lamdas: C++14 Style
13.3 C++ UG Lyon, France
13.3 C++ UG Dresden - building Compilers in C++
17.3 C++ UG Belgium - Whats new in C++14
17.3 C++ UG London
17.3 C++ UG NRW/Dortmund
17.3 C++ UG Denver - C++11 Standard
18.3 C++ UG Berlin - Peter Gottschling - Matrix Template Library
18.3 C++ UG Hamburg
19.3 C++ UG NRW/Düsseldorf - Exception Safety guarantees in C++
19.3 C++ UG North West/Seattle - C++17: I see a monad in your future
20.3 C++ UG Munich - C++ IO Streams
25.3 C++ UG Chicago seems like they need a location!
27.3 C++ UG New York - C++14

C++ and the Google Summer of Code

I wrote an overview over this years Google Summer of Code and C++

C++ and the Google Summer of Code

by Jens Weller

From the Article:

During the last few weeks I got interested in the Google Summer of Code (GSoC), as I did read some emails on the boost mailing lists about it. The Google Summer of Code is for a lot of open source projects an important opportunity to improve and extend their code base, and in 2014 it happens for the 10th time!

Range Concepts, To Infinity And Beyond

The 4th part in Erics series on ranges:

Range Concepts, Part 4 of 4: To Infinity And Beyond

by Eric Niebler

From the Article:

Last time, I introduced a new concept, Iterable, and showed how it solved many of the problems with pair-of-iterator-style ranges. This time around, I’m going to extend Iterable in small ways to make programming with infinite ranges safer and more efficient. Disclaimer: The ideas in this post are more speculative than in the previous three. I’m looking forward to the disucussion.

C++17: I See a Monad in Your Future!

Thoughts on Monads and Futures in C++

C++17: I See a Monad in Your Future!

by Bartosz Milewski

From the Article:

If you thought you were safe from functional programming in your cozy C++ niche, think again! First the lambdas and function objects and now the monad camouflaged as std::future. But do not despair, it’s all just patterns. You won’t find them in the Gang of Four book, but once you see them, they will become obvious.

Range Concepts, Part 3 of 4: Introducing Iterables

The third part of Eric Nieblers Series on ranges

Range Concepts, Part 3 of 4: Introducing Iterables

by Eric Niebler

From the Article:

In the last two blog posts, I describes the challenges I’ve encountered while building a next-generation range library. In this post, I’ll sketch for you my proposed solution: refinements of the range concepts that allow delimited, infinite, and pair-o’-iterator-style ranges to fit comfortably within the concept hierarchy without loss of performance or expressive power and with increased safety. I’ve built a range library around these concepts that subsumes and extends all of the C++98 STL algorithms and the Boost.Range adaptors, so I can say with confidence that these concepts lead to a useful and consistent generic range library.