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

标签 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.2clang 3.3 的行为相同。

所以我想知道我是否遗漏了什么或者是错误? GCC 没有提示。

最佳答案

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

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

与该问题相关的引用列表:

关于c++ - 采用默认参数的构造函数中的语法错误 `std::map`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23800790/

相关文章:

c++ - 我分配了一个指针,它自己清空了

c++ - 经过一定时间后停止 boost::io_service

c++ - 在 C++ 中通过引用传递 unsigned char 数组

长序列上的 C++ 正则表达式段错误

ios - 在越狱的 iOS 上使用 ar 或 libtool 创建静态存档失败

clang - 什么是正确的 ".clang-format"位置?

c++ - Uncaught Error : error 1114 in electron

c++ - 如何通过另一个变量的相对值来确定一个变量?

c++ - 我可以让 clang 生成函数指针的绝对地址吗?

c++ - 我可以对同一项目中的某些文件使用不同的 .clang_format 吗?