C++ 最烦人的解析

C++ 最烦人的解析,c++,most-vexing-parse,C++,Most Vexing Parse,我从你那里得到了密码 从外观上看,它应该会由于以下行而出现编译错误: TimeKeeper time_keeper(Timer()); 但只有当返回时间时才会发生存在 为什么这一行很重要,编译器会在time\u keeper(Timer())结构上发现歧义。这是由于TimeKeeper time\u keeper(Timer())被解释为函数声明,而不是变量定义。这本身并不是一个错误,但当您尝试访问time keeper的get\u time()成员(这是一个函数,不是TimeKeeper实例

我从你那里得到了密码

从外观上看,它应该会由于以下行而出现编译错误:

TimeKeeper time_keeper(Timer());
但只有当
返回时间时才会发生存在


为什么这一行很重要,编译器会在
time\u keeper(Timer())
结构上发现歧义。

这是由于
TimeKeeper time\u keeper(Timer())被解释为函数声明,而不是变量定义。这本身并不是一个错误,但当您尝试访问time keeper的
get\u time()
成员(这是一个函数,不是TimeKeeper实例)时,编译器会失败

以下是编译器查看代码的方式:

int main() {
  // time_keeper gets interpreted as a function declaration with a function argument.
  // This is definitely *not* what we expect, but from the compiler POV it's okay.
  TimeKeeper time_keeper(Timer (*unnamed_fn_arg)());

  // Compiler complains: time_keeper is function, how on earth do you expect me to call
  // one of its members? It doesn't have member functions!
  return time_keeper.get_time();
}

这是因为
TimeKeeper time\u keeper(Timer())被解释为函数声明,而不是变量定义。这本身并不是一个错误,但当您尝试访问time keeper的
get\u time()
成员(这是一个函数,不是TimeKeeper实例)时,编译器会失败

以下是编译器查看代码的方式:

int main() {
  // time_keeper gets interpreted as a function declaration with a function argument.
  // This is definitely *not* what we expect, but from the compiler POV it's okay.
  TimeKeeper time_keeper(Timer (*unnamed_fn_arg)());

  // Compiler complains: time_keeper is function, how on earth do you expect me to call
  // one of its members? It doesn't have member functions!
  return time_keeper.get_time();
}

这个答案是否回答了你的问题?这个答案是否回答了你的问题?虽然我知道标准在§13.1/3中说,在这种情况下,定时器函数类型被调整为指向函数类型的指针,但为什么有人希望首先对其进行调整?在我看来,§13.1/3造成了整个“最麻烦的解析”问题?虽然我知道标准在§13.1/3中说,在这种情况下,计时器函数类型被调整为指向函数类型的指针,但为什么有人希望首先对其进行调整?在我看来,§13.1/3造成了整个“最麻烦的解析”问题?