how can i get the type of the elements that are held by a STL container?
In what sense? Maybe using RTTI and typeid()?
Probably you have to use container::valuetype where container is the name of your container (for example std::vector)
Alek
For containers in general it will be X::value_type
. For associative containers it will be X::mapped_type
(X::value_type
corresponds to pair<const Key,T>
). It is according to Chapter 23 of C++ Standard.
To check that types are equal you could use boost::is_same
.
You need to give us more context. If you mean you want the value known at compiletime so it's easy to change it then use container::value_type
.
typedef vector<int> coordinates;
coordinates seq;
fib::value_type elem = seq.back(); // it's easy to change int type
If what you mean is that the container may hold various concrete (derived) types and you wish to know them at runtime then you should probably re-evaluate your approach. In object-oriented programming hiding the type at runtime is sometimes a powerful approach, because it means you make fewer assumptions about what you're working with. You can of course use RTTI, but there's probably a better way: we'd need more context to tell.
If you wish to compare types then you're probably heading the runtime path. C++ supports polymorphism, which is essentially that type-comparison you're looking after -- but built into the language. You want to execute a different set of instructions based on the type? Polymorphism allows you to execute a different function based on the type of the object. You need not write a single extra line of code -- only derive from a common base.
Use something like this:
if (typeid(yourVariable)==typeid(YourClass)) //...
Alek
given the types are known statically you can check they are the same statically without using rtti by using template specialization. e.g. use something like http://www.boost.org/doc/libs/1_40_0/libs/type_traits/doc/html/boost_typetraits/reference/is_same.html or if boost isn't available roll your own
Checking whether two types are the same can be achieved like this (without RTTI, value is usable at compile-time):
template <class T, class U>
struct same_type
{
static const bool value = false;
};
//specialization for types that are the same
template <class T>
struct same_type<T, T>
{
static const bool value = true;
};
//sample usage:
template <class FirstContainer, class SecondContainer>
bool containers_of_same_type(const FirstContainer&, const SecondContainer&)
{
return same_type<
typename FirstContainer::value_type,
typename SecondContainer::value_type
>::value;
}
#include <vector>
#include <list>
#include <iostream>
int main()
{
std::cout << containers_of_same_type(std::vector<int>(), std::list<int>());
std::cout << containers_of_same_type(std::vector<char>(), std::list<int>());
}
(This is basically how boost::is_same
works, minus workarounds for certain compilers.)