views:

367

answers:

7

how can i get the type of the elements that are held by a STL container?

+12  A: 
container::value_type
Samuel_xL
ok that's already great, but i can't seem compare two value types returned by container::value_type. any suggestions?
padde
@Patrick: you can't compare the "values returned by value_type" because value_type is a type, not a value. Comparing two value_types is thus a static operation, not a runtime one. Depending on what you are trying to achieve, you may want to look at "is_same" in Boost.TypeTraits: http://www.boost.org/doc/libs/1_40_0/libs/type_traits/doc/html/boost_typetraits/reference/is_same.html
Éric Malenfant
@Patrick: You asked for the *type* of elements stored in the vector, not the *value* of the elements. You can compare the values, you can't compare types (at least not without a lot of metaprogramming tricks) - perhaps you should explain what it is you're trying to do.
jalf
A: 

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

Alex Stankiewicz
+5  A: 

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.

Kirill V. Lyadvinsky
A: 

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.

wilhelmtell
A: 

Use something like this:

if (typeid(yourVariable)==typeid(YourClass)) //...

Alek

Alex Stankiewicz
or dynamic_cast<> if you would like to test inheritance chain ;)
Alex Stankiewicz
but only if your type has virtual methods.
Brett Hall
dynamic_cast is used on polymorphic pointers of course. I didn't mention it. You are right. Thanks for pointing it out ;)
Alex Stankiewicz
A: 

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

jk
+2  A: 

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.)

UncleBens
wow, that's very clever
padde