0

This is a simple code snippet that prints out bytes from a file.

void printBytes(string path) {
  ifstream input(path, std::ios::in | std::ios::binary);

  vector<uint8_t> bytes((istreambuf_iterator<char>(input)), (istreambuf_iterator<char>()));

  input.close();

  for (uint8_t b: bytes) {
    cout << hex << setw(2) << setfill('0') << static_cast<int>(b) << endl;
  }
}

Why do the arguments passed into the constructor for the vector need to be wrapped in parenthesis? When I don't include the parenthesis I get a compile error. However, they seem redundant to me and I do not understand why they are required.

I thought there exists an implicit separation between function arguments and something like this should work.

vector<uint8_t> bytes(istreambuf_iterator<char>(input), istreambuf_iterator<char>());
Jacob Schneider
  • 161
  • 1
  • 6
  • What else do you think should be correct, and why? What does that _"compile error"_ exactly tell you? – πάντα ῥεῖ Mar 15 '21 at 19:22
  • I was under the impression there is an implicit separation for each argument. Something like this `vector bytes(istreambuf_iterator(input), istreambuf_iterator());` I assumed should work. – Jacob Schneider Mar 15 '21 at 19:24
  • 1
    @JacobSchneider, I'm not sure what you mean by implicit separation, but it's exactly the same issue as one argument, just declaring a function with multiple parameters instead. – chris Mar 15 '21 at 19:40

1 Answers1

1

Here you go. Without parenthesis, it's actually a function declaration. https://en.wikipedia.org/wiki/Most_vexing_parse

eXCore
  • 307
  • 2
  • 8