问题描述
这是合法的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:
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引用的默认参数中使用的统一初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!