Talk:Template metaprogramming

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Untitled[edit]

This was redirected to Talk:Generic, but no longer needs to since template metaprogramming is now described correctly. (well, let me know if it isn't correct!-)

D[edit]

Hmm, a recent edit added TMP code for factorials in D. Since this article is very C++ish the edit is out of place with the rest of the article. Either the article should be reworked to accomodate other languages, or something else need to be done... Mikademus 11:41, 19 March 2006 (UTC)

Considering that most other languages don't support template metaprograms, it's unsurprising that C++ is the only language mentioned. And considering that and the fact that D isn't especially widely used, it probably would make sense to delete it. 68.238.86.68 14:07, 28 May 2006 (UTC)
Since no objections has been voiced I removed it per suggestion above. The D-example was redundant and contributed no further information to TMP. The article consitently uses C++ as a vehicle, which is proper since it is the utterly dominant language with TMP facilities, and if other languages should be mentioned it should be through wikilinks to the relevant section in that language's article. I also rephrased a D-advocating edit in the "benefits and drawbacks" section. Mikademus 07:03, 19 July 2006 (UTC)
If this is to be a C++ only article, it should be renamed "C++ Template Metaprogramming." But it is not so named, and it is common practice in Wikipedia to illustrate programming technique articles with how they work in various languages. The D programming language's template metaprogramming facilities are in many ways more powerful than C++'s. The D example should be put back in.
The article is not about a comparison of different languages' TMP facilities, but a discussion of TMP using C++ as the illustration. Whether D is superior or not is beside the point - wikipedia is not a place for language holy wars. The article has been rephrased to drive this point home further. The rationale behind useing C++ is that it is the dominant and most used language for this, has the most significant literature on the topic to rely opun, and thus is for all purposes the most beneficial example for the readers. Examples using other languages does not contribute to the topic, unless it is of another aspect of TMP not supported by C++. In fact, since D templates is close to being syntax-synonymous with normal functions if D was used there would be need be no need for example code. Mikademus 10:27, 22 July 2006 (UTC)
No, it's not a language holy war, simply because there's no contest. You say "The article has been rephrased to drive this point home further", I say the article has been hijacked to prevent D from showing up. If you want to talk about C++ TMP, then do that in a different article. This article is called "Template metaprogramming", not "C++ Template metaprogramming". I think the only reason you are deleting D examples is because you're ashemed of C++ template syntax. (can you prove me wrong?)
I am a strong supporter of D and I refuse to use C++. However, for the sake of sticking to the topic of template metaprogramming, I can see that having all the example code in just one language an advantage for the topic. And as C++ literature out numbers D literature by astronomical dimensions, it seems reasonable to use C++ as the language of choice for examples in this topic. Of course I'd prefer to use D because (IMNSHO) its template implementation outshines C++'s, but that is not the point here. Maybe someone can start a page to demonstrate the differences in the various template implementations out there in the wild. DerekP 11:23, 23 July 2006 (UTC)
I know very little of D, but if its template implementation offers any advantages for template metaprogramming over C++'s, then I'd like them in the article. I've searched through the history, but no edit summary includes " D ". --MarSch 19:37, 11 December 2006 (UTC)
It's been a while, but IIRC in the article version discussed above D was used just for the sake of mentioning D. The principles of TMP are the same in C++ and D; D has modified up some of C++'s template semantics (for better or worse is up to personal preferences) but the capabilities are the same. C++ is simply used in the article as it is the more recognised and common language. Any D programmer can understand and adapt the C++ code presented here, but not vice versa. Mikademus 22:05, 11 December 2006 (UTC)
I found it (http://en.wikipedia.org/w/index.php?title=Template_metaprogramming&diff=64616972&oldid=64616688). Doesn't seem to add any value. --MarSch 12:24, 12 December 2006 (UTC)

Wow! or Eeeewwww![edit]

I don't know whether to be impressed or disgusted... supposedly it was the intention of the designers of C++ to eliminate the need for macros and tricky macro-based techniques. But now it appears that when they introduced templates, they opened the door to a whole new avenue for creative, uh, hacking?

Now that I have learned about this astonishing technique, I am going to try very hard to forget it. I think I'm going to go away now and write some programs in TECO. Dpbsmith 01:41, 9 Jan 2004 (UTC)

As a counterweight to the opinion below (now above --MarSch) I have to say that I've never yet in any other language found another tool as powerful as templated metaprogramming. It has given me freedom to write incredibly terse and efficient code. Yes, it is difficult to get into, but then again, so is programming. It looks daunting, heck, it is daunting, but so are real-life powertools. Mikademus 18:27, 20 November 2005 (UTC)
As a countercounterweight to what is said below. C++ template metaprogramming is a poor man's version of a number of language features found in a wide variety of languages such as macros in Lisp. It's daunting because it's accidental, not designed, so it lacks proper support (poor error reporting, inconsistency and non-orthogonality, poor compiler implementation and so on). The fact that it's daunting has little to do with its power which is no different from that of any other language with staged computation. Sigfpe 22:00, 21 July 2006 (UTC)

Fixed top-posting of previous statements. --MarSch 19:24, 11 December 2006 (UTC)

Coincidentally I have just added a reference to a review which mentions template metaprogramming as poor Lisp macros and the nice quote. As it turns out Scheme > C++ --MarSch 19:26, 11 December 2006 (UTC)

MPSH book[edit]

The book Metaprogramming GPUs with Sh actually doesn't have anything to do with template metaprogramming. The word in the title refers to run-time metaprogramming, as opposed to compile time metaprogramming. Sh is a runtime-embedded DSL in C++.

I'm not sure exactly what do with this. As one of the authors of that book, I can say that we're now avoiding the term metaprogramming more and more, because of this common confusion.

I'm leaving in the reference for now, but someone might want to either remove or clarify it.User:Sdt [1]

I've removed it for now. --MarSch 19:28, 11 December 2006 (UTC)

Eiffel, Haskell, ML?[edit]

Eiffel, Haskell and ML are mentioned as a language supporting TMP. How?

Eiffel: Bertrand Meyer's book (2nd ed, 1997) only mentions templates in connection to C++, and manages to bad-mouth them fairly thoroughly in just one paragraph. Meyer doesn't strike me as the kind of person who happily steals successful ideas from C++, either ...

Haskell and ML: these support generic programming well, but that's not the same thing as TMP, is it? I see an external link to "Template Haskell". Following that link, it's clear that this is not Haskell, but an extension to Haskell, i.e. a different language.

JöG 17:25, 3 July 2007 (UTC)

"A macro, which is also a compile-time language feature"[edit]

In C++ macros are expanded by the preprocessor... I am not sure if this sentence is precise. Dpser 16:57, 27 September 2007 (UTC)

It is precise. Preprocessing is just a stage of source code translation, according to the C++ Standard (see the [lex.phases] section). Enerjazzer (talk) 03:47, 29 November 2010 (UTC)

Any Way to Expand Macros and Templates into code for source level debugging?[edit]

This gets around the argument that template code can be arcane/hard to understand. Basically multiple views of the same source code, and probably a nice learning tool. —Preceding unsigned comment added by BrainRepair (talkcontribs) 12:31, 16 April 2008 (UTC)

C# does not allow template metaprogramming[edit]

I removed the statement that C# does allow template metaprogramming. The person that added C# to the list also claims that Curl supports it. This should be verified by a knowledgable person in this area. Edit Reference --80.152.5.244 (talk) 08:15, 12 June 2008 (UTC)

Static polymorphism[edit]

I think the example given for "dynamic polymorphism" does not really match with what should be achieved by in static polymorphism and cannot really be replaced by it. The pattern that appears quite frequently in dynamic polymorphism (and can be replaced by static poly.) is the following: you have some clever algorithm which you put in the base class; this algorithm depends abstractly on some implementation in the derived class. So you make the implementation pure virtual in the base and fill in the implementation in the derived classes.

class Base
{
    virtual void implementation()=0; 
    public:
    virtual void clever_algorithm() { 
       ...
       implementation();
       ...
    }
};

class Derived : Base
{
    virtual void implementation(){
      // do whatever is appropriate in the derived class
    }
};

I think, this is more to the point. If nobody objects I would replace the code in the article by this code. (ezander) 134.169.77.186 (talk) 07:44, 30 September 2008 (UTC)

BTW: In the examples shown in the current article you see a huge difference in the inheritance mode used. In the dynamic polymorphism example it is public inheritance (IS-A) while in static polymorphism it is private inheritance (IMPLEMENTED-IN-TERMS-OF), which are completely differently beasts. (ezander) 134.169.77.186 (talk) 07:51, 30 September 2008 (UTC)

I assert that the term static polymorphism doesn't make sense. By definition if something is static, it is not polymorphic[1]. Static implementions are known at compile-time, and if you know which implementation to use at compiletime, then you have no need for polymorphism! The benefits of polymorphism come at runtime -- the ability use use one type as if it is another type, without knowing in advance what type you actually have. Further, there is no "is a" relationship from derived to base in the example in the article. The wording of this article should be changed to remove the term polymorphism completely. The entire section is terribly misleading and semantically incorrect. 199.172.169.21 (talk) 19:41, 4 June 2009 (UTC)

There are many types of polymorphism, and the term "static polymorphism" is widely used. If you're limiting the meaning of polymorphism to only dynamic polymorphism, you're a minority then. The article is OK, no change is required. Enerjazzer (talk) 04:33, 29 November 2010 (UTC)
I agree. From a software-engineering standpoint, "static polymorphism" describes what is going on very well (and I'm sure I've seen it used this way elsewhere). You get almost all of the benefits of polymorphism in terms of code reuse and ability to decompose a problem into logical pieces. You give up the full dynamism but that is in exchange for runtime performance and runtime type safety. In terms of the type system, strictly speaking you can't say that a vector<double> is a vector, but in a very real sense a vector<double> is a Sequence (it models the Sequence concept); similarly it is a vector<T> in that a function declared template <typename T> void foo(vector<T>& x) would accept one. —Ben FrantzDale (talk) 14:58, 29 November 2010 (UTC)

Removed terrible code example[edit]

I have removed that "static tables" code example: http://en.wikipedia.org/w/index.php?title=Template_metaprogramming&action=historysubmit&diff=382683617&oldid=378332731 . It was relying on static initialization order of instantiated entities which is left unspecified on the Standard. There is nothing stopping the compiler from initializing "long ArcTanTab<INTTYPE,SHIFT,0>::K" *after* all those n's. If that happens, all those manual assignments to it are being overwritten by all-0 again when the program starts.

It is also overall horrible and unexplained code (why was that "junk" char added - some explanation would be better)? If someone understand the matter, please fix and add it again. Static table generation, if done correctly, certainly are a good part of template metaprogramming. 87.154.137.171 (talk) 15:18, 3 September 2010 (UTC)

Someone please correct this[edit]

Hi, I was the one who had coded this part. In fact i was in a desperate need to develop static compile time tables. When i invented it (correct me if i am wrong, but internet then lacked any solution to do this). After i developed it i wanted to share it. However i never found the time to comment the same, and the example is not an example at all. Examples should be short and sweet.

Good that you took it off the page, and left a comment for me/anyone else to see.

Anyway if i find time again i will surely update it. BTW the junk character is a "static const" hence it makes the compiler assign a value to it at compile time. I could find no other way to force gcc to do it. — Preceding unsigned comment added by 117.192.116.167 (talk) 19:55, 11 June 2011 (UTC)

Hamming numbers example?[edit]

Would adding an external link to hamming-numbers.googlecode.com be useful? (Stroustrup found it a useful example of template metaprogramming). List of numbers are processed during compile time in order to calculate hamming numbers. Dgutson (talk) 08:02, 25 September 2010 (UTC)

The member of template class are mutable[edit]

Template metaprograms have no mutable variables— that is, no variable can change value once it has been initialized, therefore template metaprogramming can be seen as a form of functional programming. In fact many template implementations implement flow control only through recursion, as seen in the example below.


Template metaprograms can have mutable members. All the BOOST libraries use this concept and the member values can be changed. — Preceding unsigned comment added by 117.228.51.106 (talk) 18:29, 31 December 2015 (UTC)

Not generally, and a mutable variable wouldn't be template programming, it would be normal programming, or a combination; partial evaluation. It only becomes template progamming, if values are used to instantiate templates, and that can only be done with immutable values.Carewolf (talk) 03:21, 1 January 2016 (UTC)