Holly macaroo. This is some meta stuff.

This is a discussion on Holly macaroo. This is some meta stuff. within the C++ Programming forums, part of the General Programming Boards category; I just found out what metaprogramming is today, and my head is going to explode. Talking about using template class ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452

    Holly macaroo. This is some meta stuff.

    I just found out what metaprogramming is today, and my head is going to explode. Talking about using template class to solve recursion. This is pretty Jedi stuff. How many of you actually doing this on a regular basis?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,161
    It's not precisely recursion, it's more akin to conditional iteration.

    I've yet to encounter anyone who regularly writes new "metaprogramming" code in a production environment - the code written using these techniques will tend to be reused on a regular basis, rather than rewritten from scratch - but it is certainly nice to have when needed.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Yeah I've made a few that I re-use. The most used one is one that calculates the string length of an integer constant. In other words it does 1 + ceil(log base 10).
    One does not tend to write new ones very often, but it's fun writing them initially.
    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"

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by nimitzhunter View Post
    I just found out what metaprogramming is today, and my head is going to explode. Talking about using template class to solve recursion. This is pretty Jedi stuff. How many of you actually doing this on a regular basis?
    Depends what exactly "metaprogramming" is. If you mean using templates to generate code and/or data types at compile time, then this is a widely used technique.

    Most of the sites that give introductions to this topic tend to use really silly or unrealistic examples which leave you with the impression that this is a toy that nobody really uses. Not the case.

    The Boost library includes some very impressive examples of metaprogramming that are actually extremely useful. I think they go way off the deep end with some of it, but it's a good place to start looking at real examples of how it's done.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    thanks for the responds, guys.
    It's not precisely recursion, it's more akin to conditional iteration.
    What do you mean? I just saw this website this guy made the template class and used it just like recursive call to calculate factorial.
    Code:
    template< int i >
    class FACTOR{
      public:
          enum {RESULT = i * FACTOR<I-1>::RESULT};
    };
    
    class FACTOR< 1 >{
      public:
          enum {RESULT = 1};
    };
    Maybe, I'm missing something here.

    One does not tend to write new ones very often, but it's fun writing them initially.
    yeah, it seems like a very good practice and fun to do until I'd hit some bug and start banging my head on the table :-)

    The Boost library includes some very impressive examples of metaprogramming that are actually extremely useful.
    Those guys working on Boost are very cutting edge on C++. I'm reading the the paper from the mpl and hope that I won't get lost.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    I have talked to a university professor who does lots of C++ code reviews and he encounters a lot of template code that even he after 15+ years of C++ experience can't decipher at once and still needs a book

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Yeah, I do some meta-template programming from time to times. Lots of recursion stuff. Sometimes types.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-21-2005, 04:21 AM
  2. College stuff
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-03-2004, 11:22 AM
  3. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  4. DirectX 8.1 SDK Tutorials And Stuff
    By c++_n00b in forum Game Programming
    Replies: 3
    Last Post: 04-06-2002, 08:01 AM

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