Why no container-based algorithms?

A few minutes ago, a colleague on another team asked:

I really enjoyed your talk on Modern C++ from the Build conference, and have a quick question: Could there be a simpler syntax – something like:

foreach(collection, lambda_function) // or some other syntactic name for “foreach”

which would expand to

for_each(begin(collection), end(collection), lambda_function)

Same for find_if, etc.

This was considered and is desirable. In today’s C++ it’s easy to do for some algorithms, but not others. The main problem is overloading, which needs better template support (i.e., C++0x concepts which were proposed but didn’t make it) and/or adding enable_if.

Briefly, the basic problem is that you already have predicate overloads for some algorithms:

template<typename Iter>
void sort( Iter, Iter ); // 1

template<typename Iter, typename Pred>
void sort( Iter, Iter, Pred ); // 2

So far, so good. But what we want to add is:

template<typename Container>
void sort( Container& ); // 3

template<typename Container, typename Pred>
void sort( Container&, Pred ); // 4

And 4 is difficult to distinguish cleanly from 1 today. Both are match-anything function templates taking two parameters, and 1 would be preferred by the language when the argument types are identical, but in common cases you encounter unfortunate effects. For example, Howard Hinnant points out that if you try to call sort with (say) an iterator and a const_iterator, if we had only 1 you’d get a reasonably clear error message, but now because 4 is able to match different parameter types the compiler will instead try to invoke 4, which isn’t at all close to the original intent, and you’ll get a deeply strange template error message somewhere in the bowels of 4’s implementation because it wasn’t expecting anything like an iterator for either parameter.

Of course, not all algorithms have this issue where there are already overloads with different numbers of parameters. I’m hopeful that the standard library will get range-based overloads of all standard algorithms that are enable_if’d to avoid the problem or can use concepts if those make it into a future standard.

Here’s what the enable_if workaround might look like (I haven’t actually tried this though):

template<typename Iter>
typename enable_if< !CallableWithBeginAndEnd< Iter >::value, void >::type
sort( Iter, Iter ); // 1

template<typename Iter, typename Pred>
void sort( Iter, Iter, Pred ); // 2

template<typename Container>
void sort( Container& ); // 3

template<typename Container, typename Pred>
typename enable_if< CallableWithBeginAndEnd<Container>::value, void >::type
sort( Container&, Pred ); // 4

WordPress.com expertise

I’m generally satisfied with the look and feel of this blog, but would like to tweak it in a few small ways to get a cleaner look, nicer formatting for code examples, and such.

If you or someone you know is familiar with WordPress.com blog customization, and is interested in a small project along these lines, please send me mail. Thanks.

Steve Jobs

Today our industry is much less than it was yesterday. We have lost one of the great innovators. Even more importantly, Steve Jobs’ family has lost a husband and brother and father, and our thoughts are with them.

What can be said that hasn’t been said? Steve has been arguably the single most influential driver and shaper of personal computing in every one of its five decades, from the 1970s to the 2010s. It’s obviously true for the 1970s (Apple, Apple ][) and 1980s (Mac). As for the 1990s, it should be enough that the Mac shaped essentially all of that decade’s desktop and notebook platforms, and icing on the cake that technologies pioneered at NeXT and Pixar so heavily influenced personal gaming and other personal computing. In the 2000s, suffice it to say that Steve put the personal  i  into modern computing and again transformed this industry, and other industries. Looking forward, absent some other world-changing event, it’s clear that the rest of the 2010s will see personal computing develop along the trail he and his teams have blazed already in this decade.

Here is a measure of a man’s impact: Imagine how different — how diminished — the world would be today if Steve had passed away ten years ago.

Makes our hearts fade a little, doesn’t it?

Now imagine how different — how much more — the world would be if Steve had lived another ten years.

Or another twenty. Or another fifty, as though what we have seen were but the first half of his life — and if the second half were not as a slowly aging, diminishing man, but with his health and strength and faculties as strong as ever for that much more time, a true fifty more years.

We are all cut down too soon.

Thanks, Steve.

My two //build/ talks online

//build/

My two talks from last week’s //build/ conference are online.

My personal favorite is Writing Modern C++ Code: How C++ Has Evolved Over the Years. The thesis is simple: Modern ISO Standard C++ code is clean, safe, and fast. C++ has got a bad rap over the years, partly earned, but that’s history. This talk is a “welcome to modern C++” for programmers who may never have seen C++ before, or are familiar only with older and more difficult C++.

If you’re already a modern C++ developer you may be thinking, “but I do most of those things already, why is there a whole talk on this at a Microsoft conference?” Because as interest and use of Standard C++ is heating up again, it’s important to ensure that people have a good experience as they return to C++, or turn to it for the first time. Thus we sometimes have called this the “happy path” talk — designed to show the simple happy path through modern C++ that avoids needless pitfalls.

My other talk was Using the Windows Runtime From C++. This is Windows 8-specific and talks about the “foreign object model” language extensions used by Visual C++ 11 to talk to the new native ABI-safe WinRT types. These extensions should be used sparingly, only on the thin module boundaries around otherwise nice and portable Standard C++ code, and it was necessary to add them only because Standard C++ types aren’t ABI-safe and accessible safely from other languages. (If ISO C++ were to get a module system at some point that includes being able to talk about ABI-safe types on module boundaries, that would be happiness indeed!) There is also a template library called Windows Runtime C++ Template Library (WRL) that is another way to access basically the same functionality using template syntax; pick whichever you like best.

Thanks to all who attended!

My C++ and Beyond Intro: C++ Renaissance

Why C++

Channel 9 has just posted a recording of my intro talk at C++ and Beyond 2011 last month in Banff. Here’s the link: C++ and Beyond 2011: Why C++.

It’s a keynote-y talk, not a technical talk, but we felt it was important to address an important trend involving the language. The goal is to share a perspective and rationale for why of late there’s such a resurgence of interest in C++ — both across the industry, and within Microsoft.

Whether or not you agree with the perspective and rationale, I hope you enjoy it!

C9 interview with Scott Meyers, Andrei Alexandrescu, and me

Scott Andrei Herb at C&B 2011
Scott Andrei & Herb at C&B 2011

After the end of the C++ and Beyond event earlier this month, Charles Torre interviewed all three of us for Channel 9.

I thought it came out really well, and stayed firmly focused on C++ — including even during the parts we talked about D and other languages, where the focus was on how their best parts could be applied to C++.

Charles also taped more of the seminar, including the panels and my opening ‘keynote-y’ talk about the what’s and why’s of the C++ Renaissance. Some of those will also appear on C9 over time; I’ll blog about them as they go up.

Some highlights of this particular interview:

[00:00] Event debriefing

[01:38] Scott on C++ developers

[03:18] Modern C++

[04:17] Why D, Andrei? And what from D could and should be brought into C++?

[17:25] What problems does D solve that C++ can’t?

[22:03] C++ and D interoperability (COM is old, but COM is good)!

[24:22] C++11 and Beyond

[26:01] Herb, ISO C++ Committee’s next phase – what are you going to do? [note: see also more details in my trip report for the standards meeting held the following week]

[28:22] Scott, Andrei and Herb share perspectives on the ISO standards process, philosophies of language design, what C++ gets wrong, what it gets right

[49:48] Perspectives on this year’s event and if/when C++ and Beyond will happen again

Trip Report: August 2011 C++ Standards Meeting

The summer 2011 ISO C++ meeting was held on August 15-19 in Bloomington, Indiana, USA on the wonderful Indiana University campus. The minutes will be available at the 2011 papers page in a couple of weeks.

As previously announced, C++11 was unanimously approved just days before the standards meeting, so this was the first post-C++11 meeting. As planned, at this meeting we focused on processing some bug reports (defect reports, aka DRs) and have some initial discussion of the ‘what’s next’ variety. As expected, no decisions were made about whether we should consider new language extensions soon — that discussion will likely happen at our next meeting in February.

The big news out of last week’s meeting was on the standard library side: There was a clear decision that the library working group is ready to consider new library extensions, starting with the file system library proposal that was already accepted for post-C++11. The following announcement and instructions were read into the minutes:

The C++ committee Library Working Group welcomes proposals for library extensions which will be considered starting in the February 2012 meeting. We have not yet set out an overall timeline for future library extensions, but are ready to consider new proposals at this point.

To increase the chances of your proposal being accepted by the committee, we strongly recommend that a committee member willing to champion your proposal (this could be you yourself, or a delegate) attend upcoming meetings to help shepherd your proposal through the process.

It’s possible that this will take the form of a second library extensions technical report along the lines of the very successful Library Extensions Technical Report 1 (aka TR1). Whatever the form, it’s clear that the first order of business besides maintenance of the C++11 standard will be a new round of extensions to the C++ standard library.

Personally, I think that’s exactly what needed to happen at this meeting, and I’m very happy to see it take place. ASIO or thread pools, anyone? Maybe parallel algorithms, and concurrent containers? Stay tuned.

Looking forward

It’s our tradition to schedule one meeting a year outside the continental United States, and preferably outside North America, because this helps international participation by making it easier for people from all parts of the world to attend. Next year, as we’ve done before, this “un-American” meeting will be the Kona meeting, which is closer for folks in eastern Asia and Australia who may wish to attend.

Here are the planned dates and locations for upcoming ISO C++ standards committee meetings:

We have an international standard: C++0x is unanimously approved

[Update: “C++11” is now the confirmed name — Geneva informs me that they plan to have it published in a matter of weeks, and then we’ll have ISO/IEC 14882:2011(E) Programming Languages — C++, Third Edition. The second edition was C++03, a Technical Corrigendum, or bug patch, that contained no new features. This is the first major revision with new features.]

The final ISO ballot on C++0x closed on Wednesday, and we just received the results: Unanimous approval.

The next revision of C++ that we’ve been calling “C++0x” is now an International Standard! Geneva will take several months to publish it, but we hope it will be published well within the year, and then we’ll be able to call it “C++11.”

I want to extend my thanks again to Bjarne Stroustrup for sharing his work with the world and continuing to help move it forward, and to all of the participants whose hard work went into achieving this important milestone in the history of a great language. Thanks!

C++ Renaissance: The “Going Native” Channel

I’m happy to report there’s a new show on Channel 9 that focuses on native code development in C++. It’s called “Going Native”… iTunes podcast here, Twitter @C9GoingNative.

From the description:

C9::GoingNative is a show dedicated to native development with an emphasis on C++ and C++ developers. Each episode will have a segment including an interview with a native dev in his/her native habitat (office) where we’ll talk about what they do and how they use native code and associated toolchains, as well as get their insights and wisdom—geek out. There will be a small news component or segment, but the show will primarily focus on technical tips and conversations with active C/C++ coders, demonstrations of new core language features, libraries, compilers, toolchains, etc.

We will bring in guests from around the industry for conversations, tutorials, and demos. As we
progress, we will also have segments on other native languages (C, D, Go, etc…). It’s all native all the time.

You, our viewers, fly first class. We’ll deliver what you want to see. That’s how it works.

Go native!