@AbstractionAnan - I thought lines 15 and 19 were allocating a string of
len bytes, with each byte a null. So, when I write to
&a_[0], I know I have enough room. However, some more research does show that it is
possible for a
std::string to break by doing this, but very unlikely given that since C++11 they have to have a contiguous data representation, though admittedly a COW implementation is legal, and would probably cause this to break.
Still, that is a valid point; just because it's always worked for me... the principal stands, however, and undefined behaviour can be removed through replacing
std::string with a
std::vector<char>, copying it to a string afterwards. Interestingly enough, however, the way I did it is the way given in the example at cppreference:
http://en.cppreference.com/w/cpp/io/basic_istream/read
And yes, the
outs were a typo...
EDIT:
@SakurasouBusters
That was 'somewhat' a typo. Yes, the ampersand was deliberate - I'm passing a pointer, so I need to get the reference somehow. What
wasn't correct was taking a reference of an rvalue; again, something stupid I did. Just goes to show I need to test my code before I post it, here.
For interest's sake, here's a coliru of the program with typos removed:
http://coliru.stacked-crooked.com/a/c084f5bcc3959f73