jeudi 30 novembre 2017

What is returned from std::vector::data() when the vector is empty?

Given this example:

// "C" function prototype:
void foo(const int*);

// code:
std::vector<int> v;
foo(v.data());

Is it guaranteed by the standard that v.data() on an empty vector will produce a null pointer? I don't understand this wording:

"The pointer is such that range [data(); data() + size()) is always a
valid range, even if the container is empty (data() is not
dereferenceable in that case)."

I see that on VS2015 it does indeed produce a null pointer, but I was worried some implementations might return a pointer to some internal scratch buffer within vector.

Aucun commentaire:

Enregistrer un commentaire