David Roberts
David Roberts

Reputation: 213

Obtaining type of elements in array c++

It is easy to obtain the size of an array:

myarray.size()

Is there a function (say, type) way to obtain the type of the elements in the array?:

myarray.type()

Or must I use more mundane options?

Upvotes: 1

Views: 318

Answers (2)

alpartis
alpartis

Reputation: 1122

Your question doesn't indicate what class you're using for your array object, so strictly speaking there is no way to answer your question.

Take a look at the reference documentation for std::array from the C++ standard template library. There you can find a reference to the value_type member -- this gives you an indication of the type of object in the collection.

Upvotes: 0

quantdev
quantdev

Reputation: 23793

You can use the value_type trait, as for any STL container.

#include <array>

int main()
{
    typedef std::array<int, 5> my_array_type;
    my_array_type::value_type x = 2;
    return 0;
}

EDIT:

Yes, a type trait is of course a "compilation thing", this trait remains useful, its worth remembering that it exists on std::array.

Upvotes: 1

Related Questions