Reading and writing binary data in C++ I have written a small container class which groups a 3D position, a normal vector and a texture coordinate into one object. It uses the glm library for the actual data types (vec2 and vec3). This class is named Vertex. Here is the source:

Vertex.h

#ifndef CPP3D_VERTEX_H
#define CPP3D_VERTEX_H

#include &lt;iostream&gt;
#include &lt;glm/glm.hpp&gt;

namespace cpp3d {

class Vertex {

public:
    glm::vec3 position;
    glm::vec3 normal;
    glm::vec2 texCoord;
    Vertex();
    Vertex(const glm::vec3&amp; position);
    Vertex(const glm::vec3&amp; position, const glm::vec3&amp; normal);
    Vertex(const glm::vec3&amp; position, const glm::vec3&amp; normal, const glm::vec2&amp; texCoord);
    bool operator==(const Vertex&amp; other);
};

std::ostream&amp; operator&lt;&lt;(std::ostream &amp;stream, const Vertex&amp; vertex);
std::istream&amp; operator&gt;&gt;(std::istream &amp;stream, Vertex&amp; vertex);

}

#endif


Vertex.cc

#include "Vertex.h"

namespace cpp3d {

using namespace glm;
using namespace std;

Vertex::Vertex() {
}

Vertex::Vertex(const vec3&amp; position) :
        position(position) {
}

Vertex::Vertex(const vec3&amp; position, const vec3&amp; normal) :
        position(position), normal(normal) {
}

Vertex::Vertex(const vec3&amp; position, const vec3&amp; normal, const vec2&amp; texCoord) :
        position(position), normal(normal), texCoord(texCoord) {
}

bool Vertex::operator ==(const Vertex&amp; other) {
    return position == other.position &amp;&amp; normal == other.normal &amp;&amp; texCoord == other.texCoord;
}

ostream&amp; operator&lt;&lt;(ostream &amp;stream, const Vertex&amp; vertex) {
    stream.write(reinterpret_cast&lt;const char *&gt;(&amp;vertex), sizeof(vertex));
    return stream;
}

istream&amp; operator&gt;&gt;(istream &amp;stream, Vertex&amp; vertex) {
    stream.read(reinterpret_cast&lt;char *&gt;(&amp;vertex), sizeof(vertex));
    return stream;
}

}


As you can see I've overloaded the stream operators to write and read the data the object contains. For this I simply dump the whole object which results in writing eight float numbers so the output will be 32 bytes. I know that this is a very simple file format but I want to read the data as fast as possible so I don't want to parse text files. I also don't really care about different endiannesses (unless it can be implemented without speed impact). And I know the file format will change when I add more members to the class (This will most likely not happen but when it happens then I also want to change the file format anyway)

My main concern is if this code guarantees that it always writes and reads 32 bytes. I'm pretty new to C++ and my ancient C knowledge tells me that there is (or was) some kind of packed structs and aligned structs where the compiler could decide to align 8 bit values to 16 bits for some reason. I don't want some compiler (current or future ones) to store the 32 bit floats in 64 bit values because this obviously changes the binary data format. Can this happen with my code when compiled on specific platforms? If yes, what's the best way to write binary floats to a stream then? Shall I write each coordinate in each vector with a separate call to write()? Or are there other/better techniques to write and read the binary data?

If you see other issues with my code which are not related to my question please feel free to comment anyway. As a C++ newbie I'm eager to learn.
Both std::istream and std::ostream are actually not defined in &lt;iostream&gt;, despite the fact that they look similar.  They're respectively defined in &lt;istream&gt; and &lt;ostream&gt;.

You should instead include these in the implementation file and include &lt;iosfwd&gt; (the iostream declaration forwarding library) in the header.
These data members should be private:


glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoord;



Otherwise, they will be accessible outside of the class.  If they are not meant to be this way, then you should use a struct instead, which are public by default.
If you don't need a default constructor, then leave it out.  The compiler will provide one for you.
Regarding operator==:

Conditional operators overloads, such as operator==, should be const as they should not modify any data members:

bool operator==(const Vertex&amp; other) const;


Its definition could also be split into multiple lines, which is easier to read than one long line:

bool Vertex::operator==(const Vertex&amp; other) {
    return position == other.position
        &amp;&amp; normal == other.normal
        &amp;&amp; texCoord == other.texCoord;
}