In my variable data, when running "add a variable" script code, how to define a generic container for all types? And what is the generic formula to access them? It's annoying because I have to define a vector template for each type (int float double etc). My variable should contain only and only a generic vector object, whatever it's int, or float or double etc. Is it possible? Any idea?
-
Why do you have to define a vector template for each type? If you use C++, I think `template` works exactly by eliminating this need. Write a `template` and it should work for all types (that satisfy certain requirements.). But why do you tag this as **C**?! – phoeagon Feb 19 '13 at 11:51
-
@xersi It's not clear what you want here. Can you give a use case? Are you talking about doing `std::vector>` like in Java? – Joseph Mansfield Feb 19 '13 at 11:54
-
I don't fully understand what you need, but you may want to look at [boost::any](http://www.boost.org/doc/libs/1_53_0/doc/html/any.html). – juanchopanza Feb 19 '13 at 11:55
-
use a `boost::variant` in the container or `boost::any`. – Nim Feb 19 '13 at 11:56
-
Here's script. A template only works with its corresponding type. E.g `vector
!= vector – Feb 19 '13 at 11:59` - I have to redefine it multiple times with different types - it's annoying -
1@xersi But this is how C++ works! It's a statically typed language. The compiler *has to* decide on the type. It cannot generate something that works for arbitrary type. – phoeagon Feb 19 '13 at 12:02
-
So if my parser wants to declare a double variable, I have to define a vector double, etc for all other cases... :( – Feb 19 '13 at 12:06
-
Did you by any chance come from python, JavaScript or another similar language where this is possible? – Aesthete Feb 19 '13 at 22:30
3 Answers
That's the whole point of the Standard Template Library..
std::vector<int>
std::vector<float>
Two vectors - the same class, templated with different types.
If you want a container of differing type you might want to look at std::tuple

- 18,622
- 6
- 36
- 45
Edit:
oh, you want a container that can fit in any type? Yes you can. But within certain restrictions.
See boost::any
and boost::variant
. boost::variant
would enable you to save data of several types enumerated when declaring the boost::variant
. boost::any
doesn't make you enumerate all types you want to support, but you need to cast them to get back the value yourself.
In short, you must either store the type information somewhere else ( when using boost::any
), or just support a few types (say, a heterogeneous vector that supports int
and double
using boost::variant
)
template
in C++ works exactly by eliminating the need to write the same class for every type.
For example:
// function template
template <class T>
T GetMax (T a, T b) {
T result;
result = (a>b)? a : b;
return (result);
}
this GetMax
should work for whatever type that has a >
operator. So that is exactly what template
is for in C++.
If you need more help on implementing a vector on C++ (which, by the way, is not so simple when it comes to custom types that has its own constructor and destructor. You may need allocator
to get un-initialized space), read this (Implementation of Vector in C++).
If you want a single vector that contains objects of many different types, then you might want to use boost::any
, or possibly boost::variant
.

- 400,186
- 35
- 402
- 621