Thread: IDEA: Template meta-programming

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden

    IDEA: Template meta-programming

    Template programming is quite a challenge.

    Simple example: "Write a template struct that determines whether a number is even or odd"
    template <int num, int mod2=-1>
    struct Num
       static const bool isOdd = Num<num,num%2>::isOdd;
    template <int num>
    struct Num<num,0>
       static const bool isOdd = false;
    template <int num>
    struct Num<num,1>
       static const bool isOdd = true;
    //in main
    cout << "45 is odd: " << Num<45>::isOdd;
    The contest should be harder than that though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    I've just been reading up on this, and it seems like a good canidate. I'm not sure how many would rise to the challenge, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Pointer to template class error
    By brooksbp in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2007, 01:22 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. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM