user220878
user220878

Reputation:

Parenthesis around function call

In the code snippet below, if I exclude the parenthesis around the second call to std::istreambuf_iterator, I get a compile error on the last line:

left of .c_str() must have a class/struct/union.

std::ifstream file("file.txt");;

std::string prog(
    std::istreambuf_iterator<char>(file),
    (std::istreambuf_iterator<char>()));
prog.c_str();

What do these parentheses actually do? It seems to me that they should be able to be excluded.

Upvotes: 5

Views: 757

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258568

Without the parenthesis, this would be a case of most vexing parse. It wouldn't declare a variable, but a function returning a std::string, called prog and taking those two types as parameters. If you attempt to call it afterwards, you'll get a linker error.

Upvotes: 6

Related Questions