vendredi 29 avril 2016

How to initialize an array from a vector ? (How to cast a pointer to array ?) [duplicate]

This question already has an answer here:

I am trying to build a std::array from the data of a std::vector. I have currently done this :

#include <vector>
#include <array>

int main(void)
{
    std::vector<int>    vec1;
    std::array<int, 5>  arr{static_cast<int [5]>(vec.data())};

    (void)arr;
    return 0;
}

but gcc does not accept this cast :

error: invalid static_cast from type ‘int*’ to type ‘int [5]’

I thought an array could be used as a pointer so why cannot we do this cast ?

Aucun commentaire:

Enregistrer un commentaire