In the first program, you have a vexing parse. If the compiler can parse a declaration as either a variable or a function, it will choose to parse it as a function.
myMap
can be parsed as a function declaration.
It returns a std::map<int, int, std::less<int>>
.
It takes an argument of type std::less<int>()
, which is itself a function type that returns a std::less<int>
and takes no arguments. Note that you can't actually have a function type as an argument; the type is actually a pointer to a function that takes no arguments and returns a std::less<int>
.
In the second program, replacing ()
with {}
resolves the ambiguity. Now myMap
can no longer be a function declaration, and so it instead declares a variable of type std::map<int, int, std::less<int>>
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…