Thread: List or container to hold types

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    List or container to hold types

    Hi,

    I'm having difficulty coming up with a solution for what I am trying to achieve and was hoping maybe someone had encountered this kind of situation before.

    I am attempting to test a feature for various sorts of types, e.g. int, double, float, types that I define, etc. To do so I have created a function template that will take this type and then perform a certain operation.

    Ideally, what I would like to do is loop through the various types I have defined and pass them into this function template. But, I'm not sure what kind of container or list to use to hold these types.

    Any ideas?

    Thanks a lot for any help at all.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Lists and containers are useful because they are of flexible size; they can't hold more than one data type when they are declared.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Right, yeah that's why I'm confused about how I can achieve some sort of "list" of types.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Put them in a struct or a class. At a very basic level, a struct or class can be considered an array that can hold different types.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Depending on your needs, you could use also consider using the conversion operator, (), or a constructor to "convert" a given type into another. For example, you could convert an object of type int into an object of a rationalNumber class or an object of type double into an object of ComplexNumber, etc.. If you can convert one class into another, then you can use any of the standard containers, irrespective of the "original" type.

    Another possibility might be to devise a series of classes all derived from a single base class. If all types in the hierachy share a common behavior then you can use an array of pointer to base type to express the different behaviors for whatever types are actually in the array.
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you going to be containing types, or instances of different types? Can you have a different container for each type you want to pass to the function, or are you just wanting to instantiate the function with each different type?

    If you know the types at compile time, and you need a container of type instances, I would just have separate containers for each type. If you just want to run the function with each different type, just call the function with the different types as template parameters (e.g. myFunc<int>(); myFunc<double>(); etc.).

    If you won't know the types until runtime, you might have to use some sort of RTTI (like with typeid) to help you identify different types.

    Lastly, if you want to have your container hold instances of different types at the same time (perhaps because you want them all mixed together or in a random order), then use a container of boost::variant objects. Boost's variant is able to hold an instance of any of a group of types, and you can have a single container of variants each holding different types.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    http://en.wikipedia.org/wiki/Loki_(C_Plus_Plus)

    The Loki library implements something called TypeLists, which is a compile time linked list of types. It's been a while since I looked at it, but I *think* you can build the list at compile time, and then iterate through it at runtime. The code is a free download...someplace--I forget where. The code is explained in detail in the book "Modern C++ design" by Andrei Alexandrescu.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM