-
February 28th, 2021, 11:01 PM
#1
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.
-
March 1st, 2021, 01:59 AM
#2
Re: Template class inside a vector
Originally Posted by OneEyeMan
...
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
-
March 1st, 2021, 02:13 AM
#3
Re: Template class inside a vector
Originally Posted by OneEyeMan
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;
-
March 1st, 2021, 02:13 AM
#4
Re: Template class inside a vector
Hi,
Originally Posted by VictorN
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.
-
March 1st, 2021, 04:14 AM
#5
Re: Template class inside a vector
Hi,
Originally Posted by wolle
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
.
-
March 1st, 2021, 04:45 AM
#6
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 1st, 2021, 06:06 AM
#7
Re: Template class inside a vector
Originally Posted by OneEyeMan
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.
-
March 1st, 2021, 01:31 PM
#8
Re: Template class inside a vector
Hi
Originally Posted by wolle
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.
-
March 1st, 2021, 02:06 PM
#9
Re: Template class inside a vector
Originally Posted by OneEyeMan
...
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
-
March 1st, 2021, 02:14 PM
#10
Re: Template class inside a vector
Hi,
Originally Posted by VictorN
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.
-
March 1st, 2021, 02:21 PM
#11
Re: Template class inside a vector
Originally Posted by OneEyeMan
...
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
-
March 1st, 2021, 02:26 PM
#12
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.
-
March 1st, 2021, 02:30 PM
#13
Re: Template class inside a vector
Originally Posted by OneEyeMan
I will create a posting in the other forum.
Please, don't do it!
I'll just move this thread...
Victor Nijegorodov
-
March 1st, 2021, 02:32 PM
#14
Re: Template class inside a vector
[moved from the Visual C++ Programming forum]
Victor Nijegorodov
-
March 1st, 2021, 02:34 PM
#15
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,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|