Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error in constructor taking default argument `std::map` [duplicate]

Tags:

c++

clang

Consider simple code snippet

#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;
}

When I compile it like this: clang++ -o foo foo.cpp I face errors:

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>())
                                                                   ^

Same behaviour for clang 3.2 and clang 3.3.

So I am wondering if I missing something or is it a bug? GCC does not complain.

like image 973
GreenScape Avatar asked May 22 '14 07:05

GreenScape


1 Answers

It's a fallacy in the C++ grammar that will surprise you. I'm unsure if this has been rectified or cleared out, see below.

All major compilers accept it though, including newer versions of Clang.

A list of references pertaining to the issue:

  • Clang did not fix this because Standard says so
  • Clang fixed this for compatibility
  • The working group issue
  • a dupe question on SO
like image 80
rubenvb Avatar answered Sep 20 '22 04:09

rubenvb