CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Template class inside a vector

  1. #1
    Join Date
    Aug 2002
    Posts
    743

    Template class inside a vector

    Hi, ALL,
    What would be the best solution for the following problem:

    Let's say I have a table in the database.

    And in my C++ code I have a structure like:

    Code:
    template<tyename T> struct TableRow
    {
        int type;
        T value;
    };
    Now what I want to do is to make:

    Code:
    std::vector<TableRow> row;
    However, I am getting compiler error.

    Now I don't really need a full blown class - I just need a structure to hold a data in order to present the data.

    So what would be the best solution?

    I can create an empty structure and then use it.

    Or maybe there is a better and more elegant solution?

    Thank you.

    I'm using C++11.

    Thank you.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,961

    Re: Template class inside a vector

    Quote Originally Posted by OneEyeMan View Post
    ...
    Code:
    template<tyename T> struct TableRow
    {
        int type;
        T value;
    };
    Now what I want to do is to make:

    Code:
    std::vector<TableRow> row;
    However, I am getting compiler error.
    What error do you get?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2017
    Posts
    606

    Re: Template class inside a vector

    Quote Originally Posted by OneEyeMan View Post
    However, I am getting compiler error.
    You must specify the typename T of TableRow. Say you want it to be an int you do,

    Code:
    std::vector<TableRow<int>> row;

  4. #4
    Join Date
    Aug 2002
    Posts
    743

    Re: Template class inside a vector

    Hi,
    Quote Originally Posted by VictorN View Post
    What error do you get?
    Code:
    error C3203:  unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
    Thank you.

  5. #5
    Join Date
    Aug 2002
    Posts
    743

    Re: Template class inside a vector

    Hi,
    Quote Originally Posted by wolle View Post
    You must specify the typename T of TableRow. Say you want it to be an int you do,

    Code:
    std::vector<TableRow<int>> row;
    Well, I don't know what type it will be, hence the type field inside the structuyou.

    I will know it at run-time when I get the row.

    Thank you
    .

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,465

    Re: Template class inside a vector

    I will know it at run-time when I get the row.
    In which case you can't use templates.

    If the compiler doesn't know the type at compile time, you're very limited as to what you can do. C++ is a strictly typed language.

    If you update to a C++17 compiler, then you could consider using std::variant or std::any. But even with these, there's limitations as to what can be done at run-time.

    See https://en.cppreference.com/w/cpp/utility/any
    https://en.cppreference.com/w/cpp/utility/variant

    If the type is 'simple' (int, char, double etc), then there are things you can do with union - but this is 'bit twiddling' and not recommended.

    For COM, there's specific types including VARIANT which may be what you're after. https://docs.microsoft.com/en-us/win...iant-structure
    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++20 Compiler: Microsoft VS2019 (16.9.3)

  7. #7
    Join Date
    Feb 2017
    Posts
    606

    Re: Template class inside a vector

    Quote Originally Posted by OneEyeMan View Post
    I will know it at run-time when I get the row.
    An alternative to what 2kaud suggests is to go for an object-oriented design. In that case, you would replace T with a base class type. Then you would store objects of classes derived from the base class there. But this is quite involved, so I have another suggestion. Replace T with std::string and store all values using a string representation.
    Last edited by wolle; March 1st, 2021 at 09:55 AM.

  8. #8
    Join Date
    Aug 2002
    Posts
    743

    Re: Template class inside a vector

    Hi
    Quote Originally Posted by wolle View Post
    An alternative to what 2kaud suggests is to go for an object-oriented design. In that case, you would replace T with a base class type. Then you would store objects of classes derived from the base class there. But this is quite involved, so I have another suggestion. Replace T with std::string and store all values using a string representation.
    Yes, everything is just a set of characters.
    I think that is what I will do.

    Except that if the DB contains the number - it will be retrieved as a number and not a set of characters. So it loooks like I will need to use union with int, double and std::string.

    Thank you.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,961

    Re: Template class inside a vector

    Quote Originally Posted by OneEyeMan View Post
    ...
    Except that if the DB contains the number - it will be retrieved as a number and not a set of characters. So it loooks like I will need to use union with int, double and std::string.
    Since you get your data from a database then it is a good reason (as 2kaud already pointed out) to use the VARIANT (or the classes that encapsulate the VARIANT type, like CComVariant or, better, _variant_t)
    Victor Nijegorodov

  10. #10
    Join Date
    Aug 2002
    Posts
    743

    Re: Template class inside a vector

    Hi,
    Quote Originally Posted by VictorN View Post
    Since you get your data from a database then it is a good reason (as 2kaud already pointed out) to use the VARIANT (or the classes that encapsulate the VARIANT type, like CComVariant or, better, _variant_t)
    Never try to write something too fast and expect a good answer. And never write it when you are about to fall asleep. ;-)

    I want a cross-platform solution - Windows, *nix, Mac. Therefore I prefer to use standard C++{11}. Sorry for not mentioning it in the OP.


    Thank you.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,961

    Re: Template class inside a vector

    Quote Originally Posted by OneEyeMan View Post
    ...
    I want a cross-platform solution - Windows, *nix, Mac. Therefore I prefer to use standard C++{11}. Sorry for not mentioning it in the OP.
    Then sorry!

    However, in such a case you should have posted this question in some other C/C++ forum, NOT in the Visual C++ one!
    Victor Nijegorodov

  12. #12
    Join Date
    Aug 2002
    Posts
    743

    Re: Template class inside a vector

    Hi,
    Yes, that is what I will do.

    I will create a posting in the other forum.

    Thank you.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,961

    Re: Template class inside a vector

    Quote Originally Posted by OneEyeMan View Post
    I will create a posting in the other forum.
    Please, don't do it!
    I'll just move this thread...
    Victor Nijegorodov

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,961

    Re: Template class inside a vector

    [moved from the Visual C++ Programming forum]
    Victor Nijegorodov

  15. #15
    Join Date
    Aug 2002
    Posts
    743

    Re: Template class inside a vector

    Sorry, I just reposted it in the other forum.

    Please either delete the new posting or put this back and close it.

    Thank you and sorry for the trouble,

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)