metatemplate programming

This is a discussion on metatemplate programming within the C++ Programming forums, part of the General Programming Boards category; Have people found more interesting uses for metatemplate programming? I have mostly used it for determining constants at compile time ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    66

    Question template meta programming

    Have people found more interesting uses for metatemplate programming? I have mostly used it for determining constants at compile time rather than run-rime.

    For those not familiar what this is:
    Code:
    #include <iostream>
    
    template< unsigned N > struct tmplFactorial
    {
    	enum { val = N * tmplFactorial< N-1 >::val };
    };
    template<> struct tmplFactorial< 1 >
    {
    	enum { val = 1 };
    };
    
    #define mt_Factorial( n ) tmplFactorial< n >::val;
    
    int main()
    {
    	int x = mt_Factorial(6);
    	std::cout << "Factorial(6)=" << x << std::endl;
    	return 0;
    }
    Sample metatemplate program to generate the value of factorial(6) at compile time. Very useful if you expect parameters to change at compile time.
    Last edited by achacha; 05-21-2002 at 10:06 PM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I've heard it called template meta programming. It's pretty interesting, although the factorial example does nothing more than really add to compile time. 6! isn't a very variable constant .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    66

    template meta

    Just a sample, and in some cases I am glad to trade off compile time with runtime, in realtime apps it is critical and parameters used by these templates are usually tunnable defines (like matrix multiplication and such). Just curious if people use them for more than just that.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21