构造函数中的语法错误采用默认参数`std :: map`

Gre*_*ape 9 c++ clang

考虑简单的代码片段

#include <map>
#include <string>


struct Foo
{
    Foo(const std::map<std::string, int> & bar = std::map<std::string, int>())
        :bar(bar)
    { }

    std::map<std::string, int> bar;
};

int main(int argc, char ** argv)
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我像这样编译它时:clang++ -o foo foo.cpp我遇到错误:

foo.cpp:7:73: error: expected ')'
    Foo(const std::map<std::string, int> bar = std::map<std::string, int>())
                                                                        ^
foo.cpp:7:8: note: to match this '('
    Foo(const std::map<std::string, int> bar = std::map<std::string, int>())
       ^
foo.cpp:7:68: error: expected '>'
    Foo(const std::map<std::string, int> bar = std::map<std::string, int>())
                                                                   ^
Run Code Online (Sandbox Code Playgroud)

clang 3.2clang 3.3的行为相同.

所以我想知道我是否遗漏了某些东西或者它是一个错误?海湾合作委员会不抱怨.

rub*_*nvb 2

这是 C++ 语法中的一个会让你大吃一惊的错误。我不确定这是否已得到纠正或清除,请参见下文。

不过,所有主要编译器都接受它,包括较新版本的 Clang。

与该问题相关的参考文献列表: