advanced

Using Strings in C++ Template Metaprograms -- Abel Sinkovics and Dave Abrahams

Fresh off the press at C++ Next:

Using strings in C++ template metaprograms

by Abel Sinkovics and Dave Abrahams

Of all the reasons to love metaprogramming, probably the most compelling is that it lets us embed “little languages” in our programs... Instead of C++ expressions, of course, strings could be used to represent the DSL code snippet in the embedding C++ code. In fact, that’s an approach used by many “traditional” libraries:

auto all_caps = std::regex("[A-Z]+"); // OK

The only problem is, the contents of that string are only known at runtime, which means the language has to be interpreted at runtime, and we lose the benefit of being able to translate the structure into compiled code. The “delayed evaluation” has been delayed too long.

If, however, these strings could be parsed at compile-time, the resulting programs could have optimal performance again...

[more]

Unifying Generic Functions and Function Objects -- Dave Abrahams

From Dave Abrahams:

Unifying Generic Functions and Function Objects

I just got finished collaborating on a proposal with Faisal Vali and Herb Sutter to include generic lambdas and pythy functions in the core language. After the upcoming Portland committee meeting, we should have a good sense of how much appetite there is on the committee for including these features in C++.

While we were writing that paper, we got some of our most helpful comments and insights from Mathias Gaunard. It was a pivotal moment when Mathias reminded us that we could create operator overload sets explicitly with inheritance and using declarations, and then used it to demonstrate "overloaded lambda expressions..."

[more]

 

ASIO: Portable Stackless Coroutines in One* Header -- Chris Kohlhoff

Via C++ Next, hat tip to Dave Abrahams:

Chris Kohlhoff’s ASIO library contains an extraordinary little header, not in the public interface, but in the examples directory, that implements what he calls “Stackless Coroutines” (very similar to Python’s Simple Generators if you’re familiar with those). He does it completely portably, with just a few macros, and considering that there are zero lines of platform-specific code, they work amazingly well.

Dave cites this article that describes how to use the coroutines:

A potted guide to stackless coroutines

Keen-eyed Asio users may have noticed that Boost 1.42 includes a new example, HTTP Server 4, that shows how to use stackless coroutines in conjunction with asynchronous operations. This follows on from the coroutines I explored in the previous three posts, but with a few improvements. In particular:

  • the pesky entry pseudo-keyword is gone; and
  • a new fork pseudo-keyword has been added.

The result bears a passing resemblance to C#'s yield and friends. This post aims to document my stackless coroutine API, but before launching into a long and wordy explanation, here's a little picture to liven things up...

"Panel: Static If, C++11, Modern Libraries, and Metaprogramming" -- Alexandrescu, Meyers, Sutter

The first panel from C++ and Beyond 2012 is now available on Channel 9:

On Static If, C++11 in 2012, Modern Libraries, and Metaprogramming

Andrei Alexandrescu, Scott Meyers, Herb Sutter

Channel 9 was invited to this year's C++ and Beyond to film some sessions (that will appear on C9 over the coming months!)...

At the end of day 2, Andrei, Herb and Scott graciously agreed to spend some time discussing various modern C++ topics and, even better, answering questions from the community. In fact, the questions from Niners (and a conversation on reddit/r/cpp) drove the conversation.

Here's what happened...

[more]

User-Defined Literals, Part 1 -- Andrzej KrzemieĊ„ski

New article by Andrzej Krzemieński:

User-defined literals — Part I

This post is about the new language feature: the ability to specify user-defined literals. You may already be familiar with it, for example from N2750. In this post we will talk about literals in general, the purpose and usefulness of user-defined literals, their limitations, and alternatives in C++ to achieve similar goals.

[more]

Destructors That Throw: Evil, or Just Misunderstood? -- Jon Kalb and Dave Abrahams

Interesting new C++Next article about destructors that throw:

Evil, or Just Misunderstood?

by Jon Kalb and Dave Abrahams

From the script-style intro:

UDT (putting down drink and looking deep into CODER’s eyes): By the way, in my destructor I throw if the file fails to close properly.

CODER: I can live with that. [...] I’ve always been a sucker for dangerous types.

FADE TO BLACK

NARRATOR (V.O.): We invite you to journey with us now, into the heart of darkness, where we seek out throwing destructors and stare Evil™ in the face. Will CODER survive his encounter with exceptional destruction? The answers lie beyond the frontiers of usual best practice…

Continue reading...

Core C++, 3 of N: Overload Resolution -- Stephan T. Lavavej

Core C++, 3 of N: Overload Resolution -- Stephan T. Lavavej

Stephan T. Lavavej, aka STL, will take us on a journey of discovery within the exciting world of Core C++. We know lots of folks are either coming back to C++, coming to C++, or have never left C++. This lecture series, in n parts, is for all of you! Only STL can make that work (novice, intermediate, and advanced all bundled together and presented in a way only STL can do).

In Part 3, STL digs into Overload Resolution. A function template can overload non-template functions of the same name. In this scenario, function calls are resolved by first using template argument deduction to instantiate the function template with a unique specialization (STL taught us all about TAD in Part 2). If template argument deduction fails, the other function overloads are considered to resolve the call. These other overloads, also known as the candidate set, include nontemplate functions and other instantiated function templates. If template argument deduction succeeds, then the generated function is compared with the other functions to determine the best match, following the rules for overload resolution. [source]

As STL says: "I walk through why foo(const T&) beats foo(const T *), when given int *. The reason is surprisingly subtle."

Tune in.

Core C++, 2 of N: Template Argument Deduction -- Stephan T. Lavavej

Core C++, 2 of N: Template Argument Deduction -- Stephan T. Lavavej

Stephan T. Lavavej, aka STL, will take us on a journey of discovery within the exciting world of Core C++. We know lots of folks are either coming back to C++, coming to C++, or have never left C++. This lecture series, in n parts, is for all of you! Only STL can make that work (novice, intermediate, and advanced all bundled together and presented in a way only STL can do).

In part 2, STL will teach us all about Template Argument Deduction. Template arguments are deduced when a call is made to a template function, but some or all template arguments are omitted. The compiler will attempt to deduce the intended template arguments. In most cases, this works as expected. If it does not, a compilation error occurs, in which case you should specify the template arguments explicitly. Now, let's see what Stephan has to say about this.

Tune in. Learn.

Core C++, 1 of N: Name Lookup -- Stephan T. Lavavej

Core C++, 1 of N: Name Lookup -- Stephan T. Lavavej

Stephan T. Lavavej, aka STL, will take us on a journey of discovery within the exciting world of Core C++. We know lots of folks are either coming back to C++, coming to C++, or have never left C++. This lecture series, in n parts, is for all of you! Only STL can make that work (novice, intermediate, and advanced all bundled together and presented in a way only STL can do).

In the following code, which functions are called? Why? Analyze the implications?

 

namespace A {
      struct X;
      struct Y;
      void f( int );
      void g( X );
}

namespace B {
       void f( int i ) {
            f( i );   // which f()?
        }
        void g( A::X x ) {
             g( x );   // which g()?
        }
        void h( A::Y y ) {
             h( y );   // which h()?
        }
}


We recommend you watch this entire episode before playing around with Herb's sample above (and don't read the GotW answer, either! That's cheating. Learn from STL. He's an outstanding teacher, as you know).

Tune in. Enjoy. Learn.