0

Could someone elaborate on why the extra pair of parentheses in the code below is necessary for correct compilation? We are creating an std::ifstream and reading everything from it into an std::vector... I see these parentheses in the examples on stack overflow, but I can't figure out why they would change the way the compiler interprets the code:

#include <fstream>
#include <vector>

int main()
{
    std::ifstream curFile("file.txt", std::ios::binary);

    std::vector<char> fileContents(
        (std::istreambuf_iterator<char>(curFile)),      //why?
        std::istreambuf_iterator<char>()
    );
}

The parentheses I am talking about wrap the istreambuf_iterator constructor. Without them, the compiler seems to think that fileContents is a function declaration.

PlinyTheElder
  • 1,454
  • 1
  • 10
  • 15

0 Answers0