Thread: generics type instantiation

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    generics type instantiation

    Hello everyone,


    In C++ template, the type parameter will be deduced when we use it, and compiler will generate the speicifc version of template function/class -- called instantiation.

    In C#, it is also compiler will do the instantiation at compile time other than runtime? For example, if we use int and string for List<T>, then two versions of List class, List<int> and List<string> will be generated in IL -- which will make IL bigger? :-)


    thanks in advance,
    George

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    C++ templates and C# generics, though similar, are not the same.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Magos!


    I took some study these days. One more question about the following link,

    http://msdn.microsoft.com/en-us/libr...2h(VS.80).aspx

    For reference type, all generics types will share one instance of native code. In the following case, type Foo<Goo1> and Foo<Goo2> (suppose Goo1 and Goo2 are both reference types) will share one static member if we have one static member defined in type Foo<>?

    Quote Originally Posted by Magos View Post
    C++ templates and C# generics, though similar, are not the same.

    regards,
    George

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you mean this:
    Code:
    public class Stuff<T>
    {
      public static T Junk;
    }
    Then yes it should have one Junk per type.
    I have never used static generic variables, but put it this way: If it had only one instance, what type would it have?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Magos,


    1.

    You mean if we define Stuff<Foo> and Stuff<Goo>, then they will share different static field Junk?

    2.

    So, all different reference types in a specific generic type will share one copy of code, but different copies of static member?

    Quote Originally Posted by Magos View Post
    If you mean this:
    Code:
    public class Stuff<T>
    {
      public static T Junk;
    }
    Then yes it should have one Junk per type.
    I have never used static generic variables, but put it this way: If it had only one instance, what type would it have?

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM