C++语法歧义
考虑一下:
void f(std::pair<bool,bool> terms = std::pair<bool,bool>(1,1)) {}
gcc 4.4 没问题,gcc 4.3 抱怨 错误:在 '>' 之前预期有 ',' 或 '...'令牌。解决办法是:
void f(std::pair<bool,bool> terms = (std::pair<bool,bool>(1,1))) {}
原因是什么?是4.3的bug吗?
Consider:
void f(std::pair<bool,bool> terms = std::pair<bool,bool>(1,1)) {}
gcc 4.4 is ok, gcc 4.3 complains error: expected ',' or '...' before '>' token
. The fix is:
void f(std::pair<bool,bool> terms = (std::pair<bool,bool>(1,1))) {}
What's the reason? Is it a bug in 4.3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个已知问题。它认为第二个逗号分隔参数声明。这是因为在类定义中,函数默认参数首先仅被标记,然后仅在读取完整类主体后才进行解析。由于它并没有真正解析默认参数,所以它没有注意到逗号实际上是模板参数列表中的逗号。
请参阅http://www.open-std.org/jtc1/sc22/wg21 /docs/cwg_active.html#325 了解有关它的内容。被引用
This was a known issue. It thinks that the second comma separates parameter declarations. This comes from the fact that in a class definition, function default arguments are first only tokenized, and then only later parsed when the full class body has been read. As it thus doesn't really parse the default argument, it doesn't notice the comma is really a comma within a template argument list instead.
See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325 for something to read about it. Cited