Int2Type Generic Programming :: C++

This is a discussion on Int2Type Generic Programming :: C++ within the C++ Programming forums, part of the General Programming Boards category; The following question relates to a programming technique Andrei Alexandrescu presents in Modern C++ Design. Due to the lack of ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    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.

    Code:
    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.

    Code:
    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.

    Thanks,
    Kuphryn

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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 :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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
    Location
    Waterloo, Texas
    Posts
    5,701
    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: <>
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Kuphryn

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, 06: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

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