Template Metaprogramming with Modern C++: Templates in Depth -- Manu Sánchez

Template Metaprogramming Modern C++ in biicode

Recently on biicode:

Template Metaprogramming with Modern C++: Templates in Depth

by Manu Sánchez

From the article:

The last time  we learnt what metaprogramming was, how metaprogramming in C++ via templates works, and the functional spirit of the embedded language that C++ template metaprogramming is. In this post we will learn C++ templates in depth: Class and function templates, template parameters, variadic templates, all with in depth examples. ...

 

... Then the fact that the compiler only generates code which actually does something (All syntactic sugar that high-level constructs provide is thrown away):

int main()
{
    return Fibonacci<5>::value;
}

GCC 4.9 -std=c++11 -O0 x86 target:

main:                                   # @main
	movl	$55, 

Variadic Templates in C++ -- Eli Bendersky

plusprofilephoto.pngA nice tutorial on a feature that leads to convenient and safe calling code:

Variadic Templates in C++

by Eli Bendersky

From the article:

Prior to C++11, the only way to write functions that take an arbitrary number of arguments was to use variadic functions like printf, with the ellipsis syntax (...) and the accompanying va_ family of macros. If you've ever written code using this approach you know how cumbersome it is. In addition to being type unsafe (all type resolution has to be done explicitly with casts in va_arg, at runtime), it's also tricky to get right. The va_ macros perform low-level memory manipulation, and I've seen a lot of code that segfaults because it isn't using them carefully enough.

But what always bothered me most with this approach is leaving something that is clearly known at compile-time, to run-time. Yes, when we write a variadic function we don't know all the ways it's going to be used. But when the compiler puts the whole program together, it does know. It sees perfectly well all the invocations of the function throughout the program, and all the possible argument types it gets passed (types are, after all, resolved at compile-time in C++).

Variadic templates

One of the new features of C++11 is variadic templates. Finally, there's a way to write functions that take an arbitrary number of arguments in a type-safe way and have all the argument handling logic resolved at compile-time, rather than run-time. Variadic templates can be used for much more than just functions that take an arbitrary number of arguments; in this article I want to demonstrate some of these capabilities...

Urbana Proposals - C++17 insight?

I've started with my series on the proposals for the next C++ Committee Meeting:

Urbana Proposals - C++17 insight?

by Jens Weller

From the article:

A short series to give you an overview over the Papers submitted in the latest mailing for the C++ Committee Meeting in Urbana-Champaign in Illinois. At the beginning of November the C++ Committee will have its 3rd Meeting this year. As C++14 is now finished, the focus is clearly on the upcoming C++17 standard.

From the archives: "C++: as close as possible to C -- but no closer" -- A. Koenig and B. Stroustrup

n0007.PNGFor your Friday reading pleasure, we recently came across one of the very earliest C++ standardization papers written, with number N0007 (or call it "007"):

C++: as close as possible to C -- but no closer

by Andrew Koenig and Bjarne Stroustrup

It's interesting too see how much C++ has stayed true to its root design. And the thesis and contents of this paper are both remarkably current, and to be considered by those who would attempt to C-ify C++, or C++-ify C.

From the paper:

ANSI C and the C subset of C++ serve subtly different purposes. ...

The purpose of this note is to summarize the remaining differences between the draft ANSI C standard and C++, explain their motivation, and point out cases where these differences are less important than they might appear at first.

Italian C++ Community Meetup -- November 8, Bologna, Italy

it-meetup-2014-11.PNGThe next meetup of ++it (italiancpp.org, the Italian C++ Community) will be in Bologna, Italy, on November 8th.

Highlights

  • 1 interactive session.
  • 2x60' talks.
  • 2x30' lightning talks.
  • 1x45' Q/A panel.
  • Networking breaks and lunch.

Special speaker

  • Bartosz Milewski!

More details (in Italian) and registration (free) here.

Extension methods in C++ -- Marius Bancila

Recently on Codexpert, an enthusiastic and very readable reaction to two fresh standards proposals that will be considered two weeks from now at the ISO C++ meeting in Urbana-Champaign, Illinois, USA:

Extension methods in C++

by Marius Bancila

From the article:

... if x.f(y) and f(x,y) were equivalent it would be very easy to write the above code like this:

auto v = std::vector<int> {1,2,3,4,5,6,7,8,9};

auto s = v.where([](int e){return e % 2 == 0; })
          .select([](int e){return e*e; })
          .sum();

Isn’t that beautiful? I think it is.

...

The N4174 paper is rather an exploration of possibilities for uniform calling syntax than a very formal proposal. There are various aspects that have to be carefully considered especially when considering how to treat f(x, y). The N4165 paper makes a good case of the uniform calling syntax, explains the benefits better and argues against treating f(x) equivalent to x.f(). You should go ahead and read the two papers for detailed information. However, I sincerely hope that one day this will be accepted and become a core feature of the C++ language.