Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

share|improve this question
up vote 13 down vote accepted

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

share|improve this answer
+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

In 1994 during a meeting of the c++ standardization commitee, E. Unruh discovered that templates can be used o 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 is taken from "c++ templates" by Vandevoorde and Josuttis (chapter 17).

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

share|improve this answer
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 9:10
@iammilind What do you mean with "Is this true for SFINAE too?" – BЈовић Dec 15 '11 at 9:20
SFINAE generally needs some support from compiler. So it's possible that, it may not have been discovered accidentally. – iammilind Dec 15 '11 at 9:23
@iammilind I found an informit's article that has a similar text from the book : – BЈовић Dec 15 '11 at 9: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 9:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.