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

时间:2014-05-22 07:49:12

标签: 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;
}

当我这样编译时: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>())
                                                                   ^

clang 3.2 clang 3.3 的行为相同。

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

1 个答案:

答案 0 :(得分:2)

C ++语法中的一个谬论会让你大吃一惊。我不确定这是否已经纠正或清除,见下文。

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

与该问题有关的参考文献清单: