Thread: Int2Type Generic Programming :: C++

  1. #1
    Registered User
    Join Date
    Nov 2001

    Int2Type Generic Programming :: C++

    The following question relates to a programming technique Andrei Alexandrescu presents in Modern C++ Design. Due to the lack of time, I assume you either have read it or understand the concept via experience.

    In his book, Alexandrescu shows a generic programming technique he calls "Mapping Integral Constant to Types" in chapter 2, section 2.4. Here is an example.

    template <int A>
    struct Int2Type
       enum {type = A};
    enum TypeA{WIN = 0x0000, LOSS};
    template <typename T, int U>
    class XYZ
    T *Create(Int2Type<WIN>);
    T *Create(Int2Type<LOSS>);
    // Usage
    XYZ<MyClass, WIN> zyx;
    Again, I assume you understand the concept above.

    XYZ<MyClass, WIN> ... compiles as along as you pass in WIN or LOSS. However, here is what I want to do, but have not been able to get Visual C++ to compile without errors.

    TypeA myType;
    myType = WIN;
    XYZ<MyClass, myType> zyx;
    I would like to know if there is a solution to the problem above.

    The general idea is I want to pass in WIN or LOSS via a variable. The reason I have not implemented WIN and/or LOSS instead of the variable is because in the way I have setup the algorithm. Typing in WIN and LOSS will require switch states and if/else statements.


  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    The technique you are using is used for static dispatching on integral constants within templates. Im not sure that that is what you are doing without seeing the code. I think you should probably not need to typify an int and besides you are already using an enum and not an int. The benefit of Int2Type is that each template instantiation is a different type and you can use this to form some kind of compiletime polymorphism.Try and explain a bit better your algorithm and why you think Int2Type is appropriate.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden
    The types/values inside the < > must be constant and known compile-time.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Drax is right, a template won't compile unless the types can be resolved at compile-time.

    There is a similar concept called the "exemplar". Though instead of using templates (very generic) it allows you to generate predefined types(semi-generic) at runtime. James Coplien included a fairly large section covering this topic in his book "Advanced C++, Programming Styles and Idioms". Here's a link to the book: <>
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  5. #5
    Registered User
    Join Date
    Nov 2001
    Okay. Thanks.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic function
    By vin_pll in forum C++ Programming
    Replies: 26
    Last Post: 02-04-2009, 07:36 AM
  2. Problem with generic function
    By jlbfunes in forum C++ Programming
    Replies: 23
    Last Post: 08-17-2008, 05:13 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. Generic Host Proccess Shutdown
    By Tommaso in forum Tech Board
    Replies: 8
    Last Post: 08-14-2003, 11:18 AM
  5. help generic list and temlate
    By rip1968 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 06:28 PM