Search code examples
c++arraysc++11vector

How to determine the type of an array element?


I can't get the type of an element. This solution returns a reference to element type.

int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = decltype(*arr);
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));

Solution

  • Try the following

    using arrElemType = std::remove_reference<decltype( *arr )>::type;
    

    or

    typedef std::remove_reference<decltype( *arr )>::type arrElemType;
    

    You need to include header <type_traits>