Quick Q: Is std::array movable any better than a plan C array? -- StackOverflow

Quick A: Yes.

Today on SO:

Move constructors and std::array

According to N3485 §23.3.2.2:

(...) the implicit move constructor and move assignment operator for array require that T be MoveConstructible or MoveAssignable, respectively.

So, std::array supports move semantics if the type of its elements does. Great!

However, what does this really mean? I tend to picture this type as a safer version of an array providing an STL-compliant interface but, if this is true, then how can an std::array move-construct its elements? Can I do the same with an ordinary array?

Announcing the C++ FAQ

In addition to CppCon announced earlier this week, today the Standard C++ Foundation is pleased to announce a new unified C++ FAQ. The following introduction was written by Herb Sutter.

 

For the past 18 months, a small group of us led by Marshall Cline and myself have been hard at work on making a unified C++ FAQ available here at isocpp.org, as a wiki whose editing can be crowdsourced to keep it up-to-date with current information about modern C++.

The unified modern C++ FAQ is now ready to launch, and you can find the current content here:

C++ FAQ

The following is some background information about this project.

History and Goals

Until now, there have been several different overlapping FAQs, including notably:

  • Bjarne Stroustrup’s FAQ pages
  • Marshall Cline’s popular C++ FAQs Online
  • at least three different StackOverflow C++ FAQs in some form, last time I looked
  • and many others

However, in practice we noticed several difficulties with this status quo:

  1. Fragmentation: The existing FAQs are fragmented, so people often have to consult several of them in order to find a single answer. On the other hand, they frequently also overlap in their contents, which creates redundancy and sometimes inconsistent treatments of the same question.
  2. Staying up to date: They all (even Bjarne’s) contained some out-of-date material, because it’s just hard work for a single author or small group to maintain a good and always-updated FAQ.
  3. Legacy search rank: When people search for information about C++, search engines often lead to pages that are highly ranked because they are well established -- they have been available for a long time and are widely linked-to. But since C++11 in particular has changed modern C++ style and recommendations, much of this popular information has now become dated or incomplete.

To improve this, here are the following major goals of the new FAQ and how we are achieving them:

  1. Solve the fragmentation problem: Provide a unified merged FAQ, starting as a union of Marshall’s and Bjarne’s FAQs. Marshall and Bjarne (and Addison-Wesley) have graciously agreed to provide their FAQ contents as seed material to be the initial content of the FAQ. Marshall Cline has worked hard to import his FAQ into the new FAQ via his automated tools, and I have recently completed hand-editing over 50,000 words of Bjarne’s FAQ into the new FAQ wiki structure.
  2. Solve the up-to-dateness problem: Provide wiki-based crowdsourced editing and maintenance. Any qualified C++ expert can and should be able to add to and improve the FAQ -- including all committee members and the scores (possibly hundreds) of regular high-quality contributors on StackOverflow and other sites. Marshall Cline in particular has worked tirelessly for the past year to produce a robust wiki-based mechanism with good collision detection and resolution. (Note: Much of the imported FAQ information has already been updated, but much still needs to be done -- you can help by using the "recommend an improvement" on any FAQ title to let us know. More on that feature below.)
  3. Solve the legacy websearch problem: When people search for C++ information, they should quickly get to the latest isocpp.org FAQ. This will be simple thanks to Marshall’s and Bjarne’s cooperation: Currently much of the websearch traffic goes to their FAQs because of their long history and pagerank. Both Marshall and Bjarne will be updating their FAQs to either forward or link to the new FAQ on a per-FAQ level. (To enable this, we have maintained the internal FAQ tags Marshall was already using, and I kept a careful index mapping from Bjarne’s FAQ pages#tags to the new tags so that he can easily provide redirects.) This way, the isocpp.org FAQ should quickly become the de facto top search result for C++ FAQs and other C++ information.

cpp-faq-example.PNG

We’re also trying to provide additional features:

  • Provide new FAQ sections for community information. This includes things like "User Groups Worldwide" and "Partial List of ISO C++ Committee Members" FAQ sections.
  • Provide new FAQ sections for "What's new in C++11" and "... in C++14." This makes it easier to see "What's different if I already know [a certain previous standard supported by my compiler]."
  • Provide new FAQ sections for "C++ for {C#/Java | Objective-C | C | C++98 } developers." Some of these have already been created with seed content, but need more information from experts in those areas. We hope crowdsourcing will fill this gap relatively quickly; one new FAQ per contributor scales really well with lots of contributors while being a light load for everyone.
  • Provide a new FAQ section for "C++ Myths and Urban Legends." This will help socialize correct information by providing answers to common objections and misconceptions. C++ isn’t perfect and shouldn’t be presented that way, but it’s not nearly as imperfect as frequent incorrect claims would make it out to be. Misinformation helps no one, and we can deal with common misunderstandings here. Again, this currently contains some seed content; more to come.
  • Provide a way to subscribe to the latest/freshest information: RSS feed for FAQ changes. We've created an RSS feed for FAQ change diffs (link also available in left sidebar while in the FAQ) so that anyone can subscribe to a stream of diffs of current changes as they happen, including new FAQs and updates to existing FAQs. In the past, updates to the preexisting FAQs were often buried and undiscoverable, making them less useful than they could be. This way, people who care can easily be informed about new material, and suggest further edits.
    cpp-faq-recommend.png
  • Provide a way for anyone, not just FAQ editors, to suggest improvements. If you hover over any FAQ's title bar, you will see not only a permalink icon, but also an icon where you can "recommend an improvement to this FAQ" including to suggest new FAQs not yet in the list.
  • Provide a "moderated/curated" experience by having designated FAQ editors, but allow lots of people to become FAQ editors. Today there are already over 50 FAQ editors. If you offer good suggestions via the public "recommend an improvement" links, it probably won't be long before you start seeing Edit links appear when you visit the FAQ pages.
  • Provide a FAQ Discussion forum. For those interested in minutiae or sending suggestions directly to FAQ editors, you can subscribe to the faq-discussion forum, also available by email at [email protected] (email [email protected] to subscribe by email).

Acknowledgments

Our huge thanks go out to Marshall Cline who has put in a vast amount of volunteer work to make this possible, and also to Bjarne Stroustrup, Eric Niebler, Marshall Clow, Peter Gordon, Pearson/Addison-Wesley and the many other FAQ editors who have already made improvements -- all of these people have already contributed a lot of work and help to this. Thank you! And thanks again to everyone who has worked hard to make this possible.

Herb

 

C++11/14 Standard & Standardization--Peter Sommerlad

Peter Sommerlad on C++11 and C++14 Standard(s) and Standardization:

Peter Sommerlad on C++11 and C++14 Standard(s) and Standardization

Created by Peter Sommerlad March 6, 2014

From the presentation:

Why is C++ in again?

• more computing per Watt!
  • mobile - battery powered
  • servers - cloud computing
  • high-performance computing & GPUs
• better abstractions than C
  • without performance price (e.g. of a VM)
  • embedded (higher-level type safety)
  • security (buffer overruns, pointers)

Slides (PDF)

Effective Modern C++ book status -- Scott Meyers

Scott Meyers' work on his new "Effective C++" book, tentatively titled Effective Modern C++, progresses with an updated draft table of contents and sample Item available:

Book Report: New Title, New TOC, New Sample Item

by Scott Meyers

Highlights from the article:

I recently finished the 32nd Item for the book, thus giving me drafts of five full chapters. The math still shows that about 40 Items will fit in the book's allotted 300 pages, so yesterday I took a hatchet to the prospective table of contents and chopped the number of Items down from 51 to 41. (Why 41? Because I have a feeling that one of the Items I've written will eventually get jettisoned as not being important enough to make the final cut.)  Here's the current draft TOC....

[Table of Contents -- read it, just the titles give useful guidance]

... The most recent Item I wrote was "Distinguish () and {} when creating objects." I blogged about one aspect of this topic here, and I thought you might be interested to see what I came up with. I've therefore made the current draft of this Item available, and I welcome your comments on it. Like almost all Items I've written, it's too long, so I'm especially interested in suggestions on how I can make it shorter, but I welcome all suggestions for improvement.

Upcoming Public Presentations by Scott Meyers

Scott Meyers has announced upcoming talks for 2014:

Upcoming Public Presentations

by Scott Meyers

2014 is shaping up to be a year with more public presentations than usual. As always, you can find links to all my scheduled talks at my Upcoming Talks page, but here are the ones scheduled so far: ... there will be more to come.

Highlights from the article:

May 22, Menlo Park, CA, USA: "The Last Thing D Needs"

June 4, Oslo, Norway: "Effective Modern C++," "Type Deduction and Why You Care," "The Most Important Design Guideline," and "CPU Caches and Why You Care"

September 17, Pennsylvania, USA: "CPU Caches and Why You Care"

October 7-8, London, UK: Two-day "Effective C++11/14 Programming" seminar

String's Length -- Andrzej KrzemieĊ„ski

Today from the desk of Andrzej:

String's Length

by Andrzej Krzemieński

From the article:

Let’s start with a small test. Is the invariant expressed with the following assertion correct?

void test_length(std::string const& s)
{
  assert(s.length() == strlen(s.c_str()));
}

It is not; otherwise I wouldn’t be mentioning this in the post; but do you know why it is wrong? ...

CppCon 2014 Registration Open: September 7-12, Bellevue, WA, USA

cppcon-173.PNGThe Standard C++ Foundation is very pleased to announce the first annual CppCon.

cppcon-logo.PNG

Registration is now open for CppCon 2014 to be held September 7–12, 2014 at the Meydenbauer Center in Bellevue, Washington, USA. The conference will start with the keynote by Bjarne Stroustrup titled "Make Simple Tasks Simple!"

CppCon is the annual, week-long face-to-face gathering for all C++ users. The conference is organized by the C++ community for the community. You will enjoy inspirational talks and a friendly atmosphere designed to help attendees learn from each other, meet interesting people, and generally have a stimulating experience. Taking place this year in the beautiful Seattle neighborhood and including multiple diverse tracks, the conference will appeal to anyone from C++ novices to experts.

What you can expect at CppCon:

  • Invited talks and panels: The CppCon keynote by Bjarne Stroustrup will start off a week full of insight from some of the world’s leading experts in C++. Still have questions? Ask them at one of CppCon’s panels featuring those at the cutting edge of the language.
  • Presentations by the C++ community: What do embedded systems, game development, high frequency trading, and particle accelerators have in common? C++, of course! Expect talks from a broad range of domains focused on practical C++ techniques, libraries, and tools.
  • Lightning talks: Get informed at a fast pace during special sessions of short, less formal talks. Never presented at a conference before? This is your chance to share your thoughts on a C++-related topic in an informal setting.
  • Evening events and “unconference” time: Relax, socialize, or start an impromptu coding session.

CppCon’s goal is to encourage the best use of C++. The conference is a project of the Standard C++ Foundation, a not-for-profit organization whose purpose is to support the C++ software developer community and promote the understanding and use of modern, standard C++ on all compilers and platforms.

Looking at C++14

A few weeks ago the C++ committee did meet in Issaquah, its most important result: the final draft for C++14. See Herb Sutters trip report for details, I have written an overview based on his trip report, clangs C++14 status page and of course the papers it self.

Looking at C++14

by Jens Weller

From the Article:

As I have read through most papers of last and this year, a short overview which papers now have made it into the standard. So, what are the new features of C++14? Already before the last meeting, clang had implemented all known C++14 features of the draft published after the Chicago meeting.

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.

Stroustrup & Sutter on C++ Update

eelive.PNGUpdate: The EE Live! organizers have announced that they are able to expand the room for Stroustrup & Sutter on C++, and so seats are still available for Bjarne Stroustrup's and Herb Sutter's two-day seminar:

Super C++ Tutorial: Stroustrup & Sutter on C++

EE Live!
March 31 - April 1, 2014
McEnery Convention Center
San Jose, CA, USA

We invite you to spend two insightful and informative days with C++ luminaries Bjarne Stroustrup, the creator of C++, and Herb Sutter, the chair of the ISO C++ committee, as they address the most important issues for C++ developers in 2014.

If you are interested in attending and have not registered yet, seats are still available -- register today.

See also the session details announcement for more information about topics and content. This event is especially well timed with the technical completion of C++14 just one month ago, and will include solid and practical coverage from two of the key people who shape the C++ standard about how to effectively apply many C++14 features that are already available in your current compilers.