在const引用的默认参数中使用的统一初始值 [英] Uniform initializer used in default argument to const reference

查看:156
本文介绍了在const引用的默认参数中使用的统一初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是合法的c ++ 0x语法吗?

  class A 
{
public:
void some_function(const std :: set< std :: string>& options = {});
//注意这是合法的,它将const引用绑定到一个临时:
void some_function(const std :: set< std :: string>& options = std :: set< std: :string>());
}

因为如果是这样,我在GCC 4.6中发现了一个错误。 >

我得到的错误是:


错误:令牌


这是...逻辑...如果它是非法的。



UPDATE:正如@Kerrek所说明的,这渗入到C ++ 03中,包括聚合和旧的大括号初始化语法。为什么这不可能?是否禁止在标准?或者是错误的编译器?还是这是一个疏忽?我没有看到任何严重的问题,允许这是一个替代显式调用构造函数。

解决方案

它在C ++ 11,但它是 非常晚添加 Bjarne通过的工作文件。所以这并不奇怪,GCC不支持大括号默认参数。


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.

解决方案

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.

这篇关于在const引用的默认参数中使用的统一初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆