const 引用的默认参数中使用的统一初始值设定项
这是合法的 c++0x 语法吗?
class A
{
public:
void some_function( const std::set<std::string> &options = {} );
// note that this is legal, which binds the const reference to a temporary:
void some_function( const std::set<std::string> &options = std::set<std::string>() );
}
因为如果是这样,我刚刚发现了 GCC 4.6 中的一个错误。
我得到的错误是:
错误:“{”标记之前应有主表达式
是...逻辑...如果它是非法的。
更新: 正如 @Kerrek 所示,这会渗透到普通的 C++03 中,其中包含聚合和旧的大括号初始化语法。为什么这是不可能的?标准中是否禁止这样做?还是编译器有错?或者这是一个疏忽?我认为允许将此作为显式调用构造函数的替代方法没有任何严重问题。
Is this legal c++0x syntax?
class A
{
public:
void some_function( const std::set<std::string> &options = {} );
// note that this is legal, which binds the const reference to a temporary:
void some_function( const std::set<std::string> &options = std::set<std::string>() );
}
Because if so, I just found a bug in GCC 4.6.
The error I get is:
error: expected primary-expression before '{' token
which is ... logical ... if it was illegal.
UPDATE: As @Kerrek has illustrated, this bleeds into plain C++03, with aggregates and the old brace initialization syntax for them. Why is this not possible? Is it forbidden in the Standard? Or are compilers at fault? Or is this an oversight? I don't see any serious problems in allowing this as an alternative to explicitely calling the constructor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在 C++11 中有效,但它是 对 Bjarne 提交的工作文件非常的补充。因此,GCC 还不支持大括号默认参数也就不足为奇了。
It is valid in C++11, but it was a very late addition to the working paper that Bjarne put through. So it's not surprising that GCC doesn't support brace default arguments yet.