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
  • 21,442
  • 29
  • 112
  • 288
iammilind
  • 2,232
  • 4
  • 24
  • 35

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
  • 9,992
  • 6
  • 37
  • 56
  • 1
    +1, interesting experience. Did they talked anything about SFINAE then ? – iammilind Dec 15 '11 at 12:49
  • @iammilind: I think SFINAE is just one of the features he mentioned that were added to better support the STL, although the "SFINAE" moniker was only coined years after that. – sbi Dec 15 '11 at 12:56
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Јовић
  • 13,981
  • 8
  • 61
  • 81
  • 2
    Good one. This kind of article I have read on internet also, don't remember the source. Is this true for SFINAE too ? – iammilind Dec 15 '11 at 09:10
  • @iammilind What do you mean with "Is this true for SFINAE too?" – BЈовић Dec 15 '11 at 09:20
  • 1
    SFINAE generally needs some support from compiler. So it's possible that, it may not have been discovered accidentally. – iammilind Dec 15 '11 at 09:23
  • @iammilind I found an informit's article that has a similar text from the book : http://www.informit.com/articles/article.aspx?p=30667&seqNum=8 – BЈовић Dec 15 '11 at 09:23
  • @iammilind Edited the answer. I would say it was accidentally, because the compilers support for templates was limited. The program wasn't portable, and I think it worked only on one compiler – BЈовић Dec 15 '11 at 09:25