Uniform initializer used in default argument to const reference

笑着哭i 提交于 2019-12-01 17:24:24

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6615823/uniform-initializer-used-in-default-argument-to-const-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!