【发布时间】:2012-09-11 18:30:00
【问题描述】:
在下面的代码 sn-p 中,如果我排除第二次调用 std::istreambuf_iterator 周围的括号,我会在最后一行得到一个编译错误:
.c_str() 的左边必须有一个类/结构/联合。
std::ifstream file("file.txt");;
std::string prog(
std::istreambuf_iterator<char>(file),
(std::istreambuf_iterator<char>()));
prog.c_str();
这些括号实际上是做什么的?在我看来,他们应该能够被排除在外。
【问题讨论】:
-
你的直觉很好:他们不应该被需要。但他们是。正如@LuchianGrigore 所说,这是为了避免“最令人烦恼的解析”,这在 C++ 语法中有点小问题。
-
如果去掉括号,
std::istreambuf_iterator<char>()代表函数调用,还是内联对象实例化?这就是编译器看到的歧义,因此也是错误。 -
Vector constructor with two parameters is parsed as a function declaration 的可能重复项完全相同,只是使用向量而不是字符串。
标签: c++