CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Vector of Templates Problem

    vector which can hold any data type will maybe come in the future versions of c+
    I very much doubt it as c++ is a strongly typed language (boost::any and boost::variant are not part of the standard). c++ is defined by an ISO standard which is established via a c++ standards committee. Go ahead, try making this suggestion to them......
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #17
    Join Date
    Jul 2013
    Posts
    576

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    I want to create a vector of templates but I'm getting 2 linker errors.
    You need to add an include guard. Otherwise the compiler will generate a function in each compilation unit. The linker will then find several versions of the same function.

    One way (if you use Microsoft VC++) is to add

    #pragma once

    at the top of the file where the template is defined. Another more general way is this,

    Code:
    #ifndef SOME_UNIQUE_NAME
    #define SOME_UNIQUE_NAME
    
    // template definitions put here (and any other definitions that should appear once only)
    
    #endif
    Last edited by razzle; May 27th, 2015 at 01:21 AM.

  3. #18
    Join Date
    Jul 2013
    Posts
    576

    Re: Vector of Templates Problem

    Quote Originally Posted by 2kaud View Post
    Go ahead, try making this suggestion to them......
    It appears to have been submitted already,

    http://www.open-std.org/jtc1/sc22/wg...013/n3804.html
    Last edited by razzle; May 26th, 2015 at 10:23 PM.

  4. #19
    Join Date
    Jul 2013
    Posts
    576

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    so that means vector which can hold any data type will maybe come in the future versions of c++.
    You can store any pointer data type already in a vector since you can upcast any pointer to void*. And you can always downcast a void* pointer again to its actual type although this will not be typesafe (it can result in a runtime error if you downcast to the wrong type).

    This is the case now and it will be so in the future. Note that boost::any does not change this even if it's include in the C++ standard.

    Your problem can easily be handled by way of the so called Visitor design pattern. It's the object oriented way to accomplish typesafe downcasts.

    But you can also use the (maybe more straight forward) void* way. All you need to do is associating each void* pointer with a tag indicating the actual type (and then use the tag information to downcast each void* to its actual type when needed).

    So this is what you would store in a vector in principle,
    Code:
    struct Any {
       int tag; // does not have to be an int but should indicate the actual type of ptr 
       void* ptr; // ptr is downcasted before use to the actual type using the tag info
    };
    //
    std::vector<Any> anyVec;
    Finally, with proper design you rarely need to resort to any kind of downcasting at all. (In fact every downcast is best viewed as a design failure and you should go to great lengths to remove them.)
    Last edited by razzle; May 26th, 2015 at 10:48 PM.

  5. #20
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Wow Thank u all for their great suggestions. It's like there are hundreds of ways to accomplish the same task and I'm just getting started lol

  6. #21
    Join Date
    Jul 2013
    Posts
    576

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    Wow Thank u all for their great suggestions. It's like there are hundreds of ways to accomplish the same task and I'm just getting started lol
    Well, an include guard will take care of the linker problem.

    Regarding many types in a vector I strongly advice you to seek a solution that avoids downcasting. The most common way is to declare the vector to be of a base type and then store objects derived from that base type in the vector (or rather pointers to the objects, preferably smart pointers). This is called a polymorphic design and it's an important part of object orientation (OO).

    So there is just one way to do this really.
    Last edited by razzle; May 27th, 2015 at 12:45 AM.

  7. #22
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Quote Originally Posted by razzle View Post
    Well, an include guard will take care of the linker problem.

    Regarding many types in a vector I strongly advice you to seek a solution that avoids downcasting. The most common way is to declare the vector to be of a base type and then store objects derived from that base type in the vector (or rather pointers to the objects). This is called a polymorphic design and it's an important part of object orientation (OO).

    So there is just one way to do this really.
    I think I should write a script to thank every post since all of them are really helpful lol. Now I'm confused which method should I persuade.

  8. #23
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    I think it's okay to use templates with main() function.
    you can use templates in main()
    but you cannot turn main into a templatized function. which is what you seem to be doing in your OP.


    Just FYI since you don't seem to 'get' it.

    much simplified (it's really more elaborate than this)... template parameters in C++ are program-time placeholders for the actual type that will be decided/substituted at compile time.
    When you make a vector<T> then at compile time this results in a very specific vector-of-a-certain-type, and ALL ELEMENTS in the vector will be of THE SAME type (!!! yes, read this again 10 times so you understand this).

    The template parameter(s) are in no shape or form an indication of "this type will be decided when the executable is running". As stated before, the type has been decide/fixed at compile time and cannot change to something else at runtime.

    So if you want a vector of "at runtime decided data", you need to first create or use a class that can do just that. runtime decided data. and then you can make a vector of that type.
    there's boost::any, boost::variant, and there are other types that could serve the same purpose such as the COM VARIANT type (or the MFC/ATL wrappers thereof: COleVariant) and you can create your own type

    note that other languages have built in support for "any"/"variant" types because they derive all their types from a single 'object' type.

  9. #24
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Quote Originally Posted by OReubens View Post
    you can use templates in main()
    but you cannot turn main into a templatized function. which is what you seem to be doing in your OP.


    Just FYI since you don't seem to 'get' it.

    much simplified (it's really more elaborate than this)... template parameters in C++ are program-time placeholders for the actual type that will be decided/substituted at compile time.
    When you make a vector<T> then at compile time this results in a very specific vector-of-a-certain-type, and ALL ELEMENTS in the vector will be of THE SAME type (!!! yes, read this again 10 times so you understand this).

    The template parameter(s) are in no shape or form an indication of "this type will be decided when the executable is running". As stated before, the type has been decide/fixed at compile time and cannot change to something else at runtime.

    So if you want a vector of "at runtime decided data", you need to first create or use a class that can do just that. runtime decided data. and then you can make a vector of that type.
    there's boost::any, boost::variant, and there are other types that could serve the same purpose such as the COM VARIANT type (or the MFC/ATL wrappers thereof: COleVariant) and you can create your own type

    note that other languages have built in support for "any"/"variant" types because they derive all their types from a single 'object' type.
    So vector of templates is out of the picture. I know Python has something like that and thought maybe c++ also had some way to do it but I don't want to rely on boost so I think I'll stick with the class token method for now and then learn expression parsing algorithms. Thank you for clearing that up.

  10. #25
    Join Date
    Jul 2013
    Posts
    576

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    So vector of templates is out of the picture. I know Python has something like that
    Could you please be more specfic.

    What is this feature of Python you are looking for in C++?

    I can tell you right now that if you give up information there is no way of getting it back unless you store it somewhere. And that is regardless of programming language.

  11. #26
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Quote Originally Posted by razzle View Post
    Could you please be more specfic.

    What is this feature of Python you are looking for in C++?

    I can tell you right now that if you give up information there is no way of getting it back unless you store it somewhere. And that is regardless of programming language.
    I don't remember it cause I saw it in a forum. Anyways my question has been answered and I no longer seek a vector of templates. Thank you

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured