In the following code
#include <map>
#include <string>
struct P2d {
double x, y;
P2d(double x, double y) : x(x), y(y) {}
};
double bar() {
std::map<std::string, int> m;
//P2d lp = P2d(double(m["x"]), double(m["y"])); // this works
P2d lp(double(m["x"]), double(m["y"]));
return lp.x;
}
all compilers I tested agree the code (un-commented version) is invalid but I fail to see why the definition
P2d lp(<double>, <double>);
that I used is not acceptable.
I remember the rule was "if it can be both a function declaration and a definition then it's a declaration" but I expected that if it cannot be a declaration then it should be interpreted as a definition instead of giving an error.
What am I missing?
P2d lp((double)m["x"], (double)m["y"]);
to work. – Apparelstatic_cast
). My question is why this code is NOT working... – AnimismP2d lp(double(m["x"]), double(m["y"]));
is parsed asP2d lp(double m["x"], double m["y"]);
=>P2d lp(double* m, double* m);
– Tambourine