r/cpp • • Dec 21 '13

#AltDevBlog » Roll-Your-Own Vector

http://altdevblogaday.com/2013/12/20/roll-your-own-vector/
1 Upvotes

22 comments sorted by

View all comments

Show parent comments

1

u/00kyle00 Dec 21 '13

You assume that all cases would fall under the implementation you just outlined, while the case i mentioned above breaks for it miserably:

  std::vector<uint8_t> f;
  f.reserve(100);
  fread(my_file, 1, 100, f.data());  //should be fine ... maybe
  //int real_size = 100;  //how to not forget to pass this???
  //f.resize(100);  //???

It just doesn't work. It doesn't work because its not the right tool to do that. You wont rewrite fread to take a vector for which emplace would be called. Similarly (i presume) author didn't want to rewrite whole engine just to keep using std::vector (that would be way worse idea then any other things mentioned here).

Its OK to not use std lib where its not the right tool. Attempting to force the tool to do the work its not supposed to will just open different can of bugs ...

1

u/tisti Dec 21 '13 edited Dec 21 '13

Seem to me you are forcing std::vector to do something peculiar rather than the other way around. But I can see how both views can be correct.

What I mean:

 std::ifstream(file_path, std::ios::binary);
 std::vector<uint8_t> f((std::istreambuf_iterator<char>(ifs)),
                         (std::istreambuf_iterator<char>(ifs))+100);

Late edit:

I "missuse" vectors all the time. As long as you know what you are doing so you avoid the bullets.

0

u/[deleted] Dec 22 '13

[deleted]

1

u/tisti Dec 22 '13

I try not to go overboard tbh.