12

SFINAE and template metaprogramming can do wonderful things and many libraries also use them considerably.

Historically both of these "magic concepts" were intentionally introduced/supported in C++ ? Or they were just later discovered as useful byproducts (side products) of original template programming ?

gnat
  • 20,543
  • 29
  • 115
  • 306
iammilind
  • 2,232

2 Answers2

19

About a decade ago after a conference day I happened to find myself in a pub with Erwin Unruh and a few others and I asked him about the program VJovic mentioned. We have had a couple of beers by then, it was an unofficial meeting, and it's lived in my head for a decade, so take this with a grain of salt, but according to my memories:

He said that they had added template features in order to better serve the STL. According to him, some/many in the committee felt that they were creating something without knowing its full capabilities. He himself suspected that all the template stuff, especially so partial specialization, made up a Turing-complete language, and he always meant to do a formal proof of that, but never got around doing so.

Instead one night at a standardization meeting he came up with that program which printed prime numbers as error messages (a version that should work with modern compilers is here) that it computed during the compilation. As a template meta-program it isn't very impressive by today's standards, but it was a first, after all. Someone made what he considered a joke by formally submitting the program so it became an official ISO standardization document.

I remember that I specifically asked him about Bjarne Stroustrup's reaction to the program, and in response Erwin mimicked him by covering his eyes with his hand. :)

In hindsight I regret I hadn't asked Todd Veldhuizen, who also was at that conference, how he came up with expression templates. (He certainly was a nice chap to talk to.) But I was much younger than, and let the life-time opportunity slip by. :(

sbi
  • 10,052
11

In 1994 during a meeting of the c++ standardization committee, E. Unruh discovered that templates can be used to compute something at compile time. He wrote a program that prints prime numbers. The intriguing part of this exercise was that the production of the prime numbers was performed by the compiler during the compilation process and not at the run time. Although this program wasn't fully portable, the program did show that the template instantiation mechanism is a primitive recursive language that can perform non-trivial computations at compile time.

This excerpt is taken from "c++ templates" by Vandevoorde and Josuttis (chapter 17).

As you can see, it was discovered early and its power was recognized immediately.

BЈовић
  • 14,049