samedi 3 mars 2018

Constructing a vector from range defined by pointers of a smaller type

In the following code, a vector<int> is constructed from an array of int8_t. It happens to work, but is it safe?

int8_t *arr;
int arr_size;
... // Filling the array somehow
std::vector<int> v(arr, arr + arr_size); // Is this safe even though we use int8_t* to construct a vector of int?

The documentation at cppreference.com says:

This constructor has the same effect as vector(static_cast<size_type>(first), static_cast<value_type>(last), a) if InputIt is an integral type. (until C++11)

This overload only participates in overload resolution if InputIt satisfies InputIterator, to avoid ambiguity with the overload (2). (since C++11)

This explanation does not give me a clue about my question. In fact, it confuses me further, since the template arguments for static casts seem to be very wrong...

Aucun commentaire:

Enregistrer un commentaire