an array of templates?

This is a discussion on an array of templates? within the C++ Programming forums, part of the General Programming Boards category; I hope someone with broad knowledge about templates and arrays can direct me on this. i know how to make ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    12

    Question an array of templates?

    I hope someone with broad knowledge about templates and arrays can direct me on this.

    i know how to make a simple template class and use it for different datatypes. i have little knowledge about vector-containers. my question is if it can be possible to have a "container" (im not sure if it is the right definition to use) that stores different datatypes in one array?

    if you dont get it i will try make some C++/pseudo-code thing so its easier to understand what im looking for:
    Code:
    container->AddThing<char> (100);
    container->AddThing<int> (4000);
    container->AddThing<myDataType> (MyDataType(100, 50));
    the class of container:
    Code:
    {
        //has a vector?
        std::vector<SomeClass*> array;    //here there must be stored different datatypes
    
        template <class T> void AddThing(T value)
        {
            array.push(value);
        }
    }
    so the array should be able to store chars, ints, etc, and also own datatypes(structures). for example on the example above the memory-layout or array would look like this (if it really matters):

    array[0] = 100;
    array[0+4] = 4000;
    array[0+4+DataTypeSize] = 100 and 50;

    the memory layout is really not that important for me before I get something to work, i am really just out after storing a template (different datatypes) into an array, so i can easilly access the members with the dot or pointer operator, or by using an iterator.

    i need some direction, or what to look for to find the right documentation i can read to learn this, if C++ are able to do this?
    (edit: also you see i have SomeClass as the parameter in the vector member, i wonder if SomeClass should be a pure virtual class or something. i am still not a pro in understanding pure virtual classes or functions and if this will be the best way to do it, if its even possible?)
    Last edited by c coder; 02-10-2012 at 09:36 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    No, it is not possible to instantiate a template in order to create a container that contains a mix of datatypes, since templates must be instantiated at compile time (i.e. the compiler must be told what types and parameters to instantiate the template for - you can't wait until runtime to instantiate a template with different types/parameters). The standard containers are template classes, so containers (and the types they contain) can only be specified at compile time.

    More significantly, it is not possible to change the return type of a function arbitrarily depending on runtime data. The return type of a function can only be determined at compile time. This doesn't change with templates, given the requirement (as above) to instantiate the template at compile time.

    How about you explain WHY you have decided you need to do this? It would be unusual to have a requirement for arbitrary data types to be stored contiguously in a single container, that can be stored arbitrarily and and retrieved correctly in arbitrary manners without any bookkeeping (to keep track of what type is stored in what part of the container). Such requirements usually have an element of need for "magic happens somewhere", which is rather hard to code in C++ without making specific assumptions.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Quote Originally Posted by grumpy View Post
    How about you explain WHY you have decided you need to do this? It would be unusual to have a requirement for arbitrary data types to be stored contiguously in a single container, that can be stored arbitrarily and and retrieved correctly in arbitrary manners without any bookkeeping (to keep track of what type is stored in what part of the container). Such requirements usually have an element of need for "magic happens somewhere", which is rather hard to code in C++ without making specific assumptions.
    Ive decided to do this because of easiness of maintaining and updating code. Since i vant arbitrary datatypes and even own datatypes as in structures. Later when I want a new datatype-structure having to update code that allready does the same thing using templates was/is the thing i wanted to do. Having to bookkeep them would be painfull because of my design. (Id had to change alot on the code and would be too time consuming, and every time i needed a new datatype). I allready have a template class that manipulates the data that is supposed to be putted into some sort of an array. Because all data in this array are supposed to be updated in a loop. And those datas have different datatypes.
    Last edited by c coder; 02-10-2012 at 11:16 PM.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by c coder View Post
    Ive decided to do this because of easiness of maintaining and updating code. Since i vant arbitrary datatypes and even own datatypes as in structures. Later when I want a new datatype-structure having to update code that allready does the same thing using templates was/is the thing i wanted to do. Having to bookkeep them would be painfull because of my design. (Id had to change alot on the code and would be too time consuming, and every time i needed a new datatype). I allready have a template class that manipulates the data that is supposed to be putted into some sort of an array. Because all data in this array are supposed to be updated in a loop. And those datas have different datatypes.
    That doesn't make much sense ..(to me at least) .
    Are you sure that you are not over designing and loosing track of what OOP should do ?

    If the types you're going to put in the 'array' have some similarity regarding what they're to represent, make a base class and store references to its objects in the array.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    manasij: Maybe it didnt make sense because my explanation was very subjective. and the core problem is more complex than that. Doing so by pasting alot of code wouldnt help much either. I was trying to do that with the pseudo-code thingy up there.
    Are you sure that you are not over designing and loosing track of what OOP should do ?
    That's exactly what im not trying to do.
    If the types you're going to put in the 'array' have some similarity regarding what they're to represent, make a base class and store references to its objects in the array.
    Yes, they have some similarity. Your suggestion is allready something i've considered looking into. It might work, but i have a hunch its not really what i am out after. I will try if its my last option though. Thanks for the feedback so far. I think i've gotten the answers I needed. That it is not possible or there are noone who can direct me by storing arbitrary datatypes using templates for an array. If its even possible. If it's not possible, then I'd just have to deal with it using other solutions that won't require alot of coding each time i implement a new datatype.
    Last edited by c coder; 02-11-2012 at 01:45 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    As you've described it, there is no solution.

    That's why I asked you to clarify what you are actually trying to achieve. Your response suggests you have decided you want variant data types, without bookkeeping. But you can't seem to answer my question (what would that "solution" allow you to achieve?) and you also can't explain how variant data types would achieve it.

    I can assure you that code is very hard to maintain and update if you can't articulate its purpose, or articulate how it logically meets that purpose better than some other design or coding approach.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    If all you need to do is guard against type changes in the code, then use typedefs. For example, your function takes a MyType_t which you can also use as the type for your containers.
    Alternatively, you can also use decltype, which will let the compiler determine the type of some variable which you can use in your containers:

    int foo;
    std::vector<decltype(foo)> bar;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates: What does this do?
    By NeonBlack in forum C++ Programming
    Replies: 4
    Last Post: 07-14-2011, 08:24 PM
  2. Creating an array with templates
    By Subsonics in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2010, 11:36 AM
  3. Templates
    By rodrigorules in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2009, 10:46 AM
  4. help with templates
    By drdodirty2002 in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2004, 05:25 AM
  5. Need help with templates
    By Flyer in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2003, 10:12 AM

Tags for this Thread


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