advanced

User-Defined Literals, Part 3 -- Andrzej Krzemieński

Andrzej Krzemieński’s latest:

User-defined literals — Part III

In the previous post we have seen how we can define a raw literal operator template that enables us to convert almost any binary literal of the form 11011_b to a corresponding value of type unsigned int at compile time and still use this value as a compile-time constant. However, the length of the literal has to be short enough to fit into the capacity of type unsigned int.

In this post, as promised, we will try to make our literal render values of different types based on the length of the binary literal, so that 11011_b renders value of type unsigned int and 100010001000100010001000100010001000_b renders value of type long long unsigned int ...

Core C++, 5 of N: Explicit and Partial Specialization -- Stephan T. Lavavej

Core C++, 5 of N: Explicit and Partial Specialization -- 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 5, Stephan teaches us about Explicit and Partial Specialization of class and function templates.

From MSDN ->

Class templates can be specialized for specific types or values of the template arguments. Specialization allows template code to be customized for a specific argument type or value. Without specialization, the same code is generated for each type used in a template instantiation. In a specialization, when the specific types are used, the definition for the specialization is used instead of the original template definition. A specialization has the same name as the template of which it is a specialization. However, a template specialization can be different in many ways from the original template. For example, it can have different data members and member functions.

Use specialization to customize a template for a specific type or value. Use partial specialization when the template has more than one template argument and you only need to specialize one of them, or when you want to specialize behavior for an entire set of types, such as all pointer types, reference types, or array types.

// explicit_specialization1.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

// Template class declaration and definition
template <class T> class Formatter
{
   T* m_t;
public:
   Formatter(T* t) : m_t(t) { }
   void print()
   {
      cout << *m_t << endl;
   }
};

// Specialization of template class for type char*
template<> class Formatter<char*>
{
   char** m_t;
public:
   Formatter(char** t) : m_t(t) { }
   void print()
   {
      cout << "Char value: " << **m_t << endl;
   }
};

int main()
{
   int i = 157;
   // Use the generic template with int as the argument.
   Formatter<int>* formatter1 = new Formatter<int>(&i);

   char str[10] = "string1";
   char* str1 = str;
   // Use the specialized template.
   Formatter<char*>* formatter2 = new Formatter<char*>(&str1);

   formatter1->print();
   formatter2->print();
}

Adventures in Perfect Forwarding--Scott Meyers

From Scott Meyers' blog:

On Saturday, June 2, Facebook sponsored a one-day C++ conference and asked me (and others) to give a presentation.  I chose an abridged and updated version of a talk from C++ and Beyond 2011, "Adventures in Perfect Forwarding."  Judging by the dates on the comments below the video, it's been available since July, but I found out about it being online only today...

Read more at Scott's blog.

User-Defined Literals, Part 2--Andrzej Krzemieński

Andrzej Krzemieński's latest:

User-defined literals -- Part II

In the previous post on user-defined literals, we have seen what user-defined literalsare for and how you define a cooked literal operator, i.e., where compiler that sees literal 12_kg extracts value 12 of type long double and calls your function operator"" _kg(12.L) to transform the result.

In this post we will explore other aspects of user-defined literals: raw literal operators, which allow you to inspect every character in the literal...

Parameter Types in Constructors -- Scott Meyers

From the keyboard of Scott Meyers:

Parameter Types in Constructors

by Scott Meyers

I recently went through Sumant Tambe's presentation materials from his Silicon Valley Code Camp presentation, "C++11 Idioms."  He argues that an emerging idiom is to pass arguments to constructors by value, because this takes advantage of move opportunities when they are available.

I was surprised to read about this emerging idiom, in part because I had not heard of it (I'm supposed to be clued in about this kind of stuff) and in part because it runs contrary to my own thinking on the subject, which is to use perfect forwarding...

Universal References in C++11 -- Scott Meyers

Recorded live at C++ and Beyond 2012:

Universal References in C++11

Scott Meyers

Given that rvalue references are declared using "&&", it seems reasonable to assume that the presence of "&&" in a type declaration indicates an rvalue reference. That is not the case...

In this article, I describe the two meanings of "&&" in type declarations, explain how to tell them apart, and introduce new terminology that makes it possible to unambiguously communicate which meaning of "&&" is intended. Distinguishing the different meanings is important, because if you think "rvalue reference" whenever you see "&&" in a type declaration, you'll misread a lot of C++11 code...

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...