c++ - memcpy underlying data from std::vector of objects -


is safe or happen work on current compiler? there in standard? result in floats vector correct.

class color { public:   color(float r, float g, float b, float a) : mcolor{r,g,b,a} {};    inline const float *data() const   {     return mcolor;   }  private:   enum {vectorsize = 4};   float mcolor[vectorsize]; };  //test std::vector<color> colors(2); std::vector<float> floats(8); colors[0] = color(0.1, 0.2, 0.3, 0.4); colors[1] = color(0.5, 0.6, 0.7, 0.8); memcpy(floats.data(), colors.data(), 8 * sizeof(float)); 

it's guaranteed work

from standard

23.3.6.1 class template vector overview

a vector sequence container supports random access iterators. in addition, supports (amortized) constant time insert , erase operations @ end; insert , erase in middle take linear time. storage management handled automatically, though hints can given improve efficiency. elements of vector stored contiguously, meaning if v vector t type other bool, obeys identity &v[n] == &v[0] + n 0 <= n < v.size().

all pods trivially copyable


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -