Thread: Template metaprogramming, whats the point?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    62

    Template metaprogramming, whats the point?

    So when I first read about template metaprogramming I said. "Humm, this is interesting!" But the more I read about it, the more it seems a that most of its functionallity could be replaced with a iterative for loop and a command to the compiler"unroll loops". Beyond that, its uses seem pretty trivial.

    Am I missing something here? Is this like Bayes theorem where you REALLY have to get it before you see the uses for it?
    Last edited by Cogman; 01-27-2009 at 08:25 PM. Reason: woops, typed the wrong thing!

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    most of its functionallity could be replaced with a iterative for loop and a command to the computer "unroll loops"
    erm, absolutely not. loops are runtime code. templates are compile time structures.

    the point of templates are that they allow you generalize patterns and algorithms for different types without having to recode them individually or rely on less-safe and flexibile techniques like downcasting.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Boost includes a library to facilitate metaprogramming, and along with it they provide a very good introductory tutorial (at least I thought so when I read it). I don't actually pretend to understand how it all works, but I think you should give it a read.

    http://www.boost.org/doc/libs/1_37_0...functions.html

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    So when I first read about template metaprogramming I said. "Humm, this is interesting!" But the more I read about it, the more it seems a that most of its functionallity could be replaced with a iterative for loop and a command to the computer "unroll loops". Beyond that, its uses seem pretty trivial.
    O_o

    What garbage have you been reading?

    Am I missing something here?
    Yes. Meta-programing, of any sort, isn't about saving a few cycles during execution. Meta-programming is about making a tool do the job of a programmer. It is about generation. The result of the application of meta-programming, the expansion, may save a few cycles during execution, but the same performance could have been had by manually writing the source in the first place.

    Soma

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by m37h0d View Post
    erm, absolutely not. loops are runtime code. templates are compile time structures.

    the point of templates are that they allow you generalize patterns and algorithms for different types without having to recode them individually or rely on less-safe and flexibile techniques like downcasting.
    Im really sorry, slip of the keyboard. I ment to say compiler rather then computer. I know that GCC supports a loop unrolling optimization which seems to eliminate much of the use for metaprogramming.

    I thought about complex recursion being another case where it could come in handy, but by that point the fact still remains that you have to have a specific target for it to work, Most of the complex uses for recursion, that I know of, would be pretty useless if they where purely static (IE tree traversing).

    I guess what I am saying, is that I haven't seen any good examples of metaprograming online. The only examples that seem to be present are factorials and Fibonacci numbers.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    nope. computer/compiler - it doesn't matter. loops are for the processing of data at runtime.

    templates and metastructures are compile time structures. loops perform operations on data that has been allocated once the program is running. templates can determine both what data gets allocated, and how to deal with it.

    loops and templates are in no way equivalent; there's a conceptual disconnect.

    i am hardly a metaprogramming guru, but i did recently implement a rather nice (imho) metastructure.

    consider this:

    http://cboard.cprogramming.com/showthread.php?t=111075

    you could use a typelist metaobject structure like the kind i'm toying with there to represent a database table without having to recode structs for each table in the schema. you could just use a typelist to create a structure that encapsulates the columns of the table.

    this isn't the only way to go about doing it, but it is arguably a good one.


    fibbonacci numbers and factorials are fundamentally NOT suited for templates - not in the slightest. they only use 1 datatype: integers.

    there is no purpose whatsoever to using a template for an algorithm that inherently uses only 1 datatype.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the more classic example is a generalized functor.

    do you want to have to code different functors for each function signature? that is so cumbersome as to make the use of functors impractical. however, with typelists and a functor metaobject, they become elegant, robust, and convenient.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by m37h0d View Post
    fibbonacci numbers and factorials are fundamentally NOT suited for templates - not in the slightest. they only use 1 datatype: integers.

    there is no purpose whatsoever to using a template for an algorithm that inherently uses only 1 datatype.
    Fibonacci is not suited for template metaprogramming, because you usually need the the first portion of the series series, not a single element to the series, so you might as well use a loop. But there are uses for metaprogramming that does not involve different datatypes, or type parameters.

    For example, You can use templates to implement as a lookup table for complex expressions that can be computed compile time, that the compiler is not otherwise smart enough to optimize, and without requiring the programmer to manually do the calculation himself.

    An ideal example would be a hash function. Metaprogramming can be used to create a hash of a sequence of characters like a password, that can be used to verify the password, but not retrieve it. Unfortunately, the syntax for this is atrocious, since the sequence of character cannot be written as a literal string.
    Last edited by King Mir; 01-27-2009 at 10:59 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Re: fibonacci - aah, yes, i suppose so. i recall seeing that example now.
    Last edited by m37h0d; 01-27-2009 at 10:52 PM.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I realized that part had a major typo, but I hope the negatives later in the sentence made it clear that I actually meant not suited. The OP is correct that that example is not the best application, though it does demonstrate the principle.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We use template meta programming for a very useful purpose at work. We often have constants defined for the minimum and maximum values a given field can take. Previously we also had a seperate constant for the maximum string length of the maximum constant. This was used in limiting an edit box to a fixed number of characters. However over time, sometimes the maximum was changed, but the number of characters was not. E.g. a certain field might have once allowed values up to 50000, but was later restricted to just 4000, and the constant for the string length was left at 5.
    We now have a template meta-programming function surrounded by a macro such you can replace MAX_FIELD_LENGTH with CONSTANT_DIGITS(MAX_FIELD_VALUE) and now, never again can the constants become out of sync.

    Other useful things are some template meta-programming to tell you at compile time if a number is prime or not. combined with a compile-time assert, you get the ability to change the chosen prime between versions, with the safety in knowing that it will never compile with a non-prime.

    I've also needed to calculate the log base b of a number n at compile time, where n and b are provided template parameters affecting the maximum size and memory/speed tradeoff values.

    Trust me, it has it's uses in the real world.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Anything that is iterative or recursive and has a know start compile-time value can be optimized with template meta programming... It will cut the whole thing out from runtime with a hard-coded constant by the compiler. Easily a good example, albeit a small one. Log, exp, etc are good algorithms that work well with it, provided you do have a known value at compile time.

    I was also reading expressions templates. They look a lot like tmp actually, and has a huge advantage.
    The resulting code can actually be faster than hand-coded C that uses callbacks.
    http://ubiety.uwaterloo.ca/~tveldhui.../exprtmpl.html

    It sure does demonstrate the power of template and template programming techniques.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also, calculating things at compile time is just one aspect of template metaprogramming.

    For example, consider something like the BOOST_FOREACH macro (thankfully it will be replaced with a built-in construct). There is actually a lot of type-deducing going on underneath to support all the iterable types: C style strings, arrays and classes having a begin() and end() method.

    Code:
    #include <iostream>
    #include <vector>
    #include <boost/foreach.hpp>
    
    int main()
    {
        char* str = "Hello!";
        int arr[] = { 1, 2, 3, 4, 5 };
        std::vector<int> vec(3, 42);
    
        BOOST_FOREACH( char c, str ) {
            std::cout << c << ' ';  //H e l l o !
        }
        std::cout << '\n';
    
        BOOST_FOREACH( int n, arr ) {
            std::cout << n << ' '; //1 2 3 4 5
        }
        std::cout << '\n';
    
        BOOST_FOREACH( int n, vec ) {
            std::cout << n << ' '; //42 42 42
        }
        std::cout << '\n';
    
        /*
        doesn't compile:
        int* p = arr;
        BOOST_FOREACH( int n, p ) {
            std::cout << n << ' ';
        }
        */
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    per this thread, i just optimized my spectrum classes to be template<unsigned int width>spectrum instead of allocating a new array of doubles off the heap.

    now i dont' have do to size-checking on all my arithmetic and assignment operators. compile-time errors ftw!

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    So, if I'm getting this correct. The best use for template metaprogramming is when you have some value in your program, you know will be static, and you know can be completed in some time (not an infinite loop). Basically, template metaprogramming will get that value for you, doing all the processing at compile time and then just do something to the effect of a quick mov call.

    Correct? So the main use is in a situation where, perhaps you need to know constant value but all you have is the formula and the data to get the value. And since the data changes frequent enough, a template of the formula will do the calculation for you.

    Off the top of my head, I don't know where I would use this, but I guess it is probably pretty useful, especially in complex calculations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM